If a user extracted the files of my php application to example http://example.com/test/webapp/
, the absolute links in my application won't work anymore.
How can I get the path /test/webapp/
to add it to my absolute links?
You can with:
dirname( $_SERVER["SCRIPT_NAME"] );
make sure your links are always relative in your pages, so when you move your site to another domain they still work.
Check out all other $_SERVER
variables by executing phpinfo()
.
for redirecting, you can use:
header('Location: https://' . $_SERVER['SERVER_NAME'] . dirname($_SERVER['SCRIPT_NAME']) . '/some_page.php');
I think you need $_SERVER['REQUEST_URI']
, but it's not a full solution. It gives you the relative path, but it's not guaranteed that you can add that. /documents
and /documents/index.php
, may both point to the same script, but cannot both be used to just add a relative path to. I think it's best to make it a setting, which the user may configure.