The thing is you can start from the DOCUMENT_ROOT and work your way from there, but if you are using Apache’s VirtualDocumentRoot, it is not reliable. Hence, you need to guess it.
The trick resides in SCRIPT_NAME and SCRIPT_FILENAME that respectively describe the executed PHP file, starting from the domain, and its full path. I used exclamation marks as delimiters as I doubt you have some in your folder names. If you do, then… shame on you.
While I was at it, I added some port and https detection and the absolute URL.
Variable | Content |
---|---|
base_dir | /var/www/mywebsite |
doc_root | /var/www |
base_url | /mywebsite |
protocol | http |
port | 8080 |
domain | example.com |
full_url | http://example.com:8080/mywebsite |
Not included
- Port detection of a server running behind a proxy. You may want to use the port of the proxy, not the Web server.
- Same goes for https.
- Non-resolved symlinks and relative paths. You may want to throw a couple of realpath() in there.
No comments:
Post a Comment