I have a PHP script that needs to know it's URL, even if it's included within another script. I've tried:
$_SERVER['REQUEST_URI']
But this doesn't return the URI to my script--rather, it returns the URI to the including script. I've also tried:
dirname ( __FILE__ )
But I can't seem to convert this to an absolute URL.
Can my script find its own URL, even if it's being included in another script? Thanks for your help!
I think you can't, at least not in a robust and portable way. PHP encourages developers to think that the URL maps directly with a script, so one may be convinced that for each script S there's a function(S) which gives its public URL.
Unfortunately, this is not true. As a quick reference, just think of mod_rewrite
or the chance that your script may be requested by a reverse proxy.
If you really need this functionality, it could be that you have a design mistake, so if you tell what your problem is exactly (in terms of functionality, not implementation) someone may come up with the right solution for you.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With