Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the URL of current script even if it's included?

Tags:

php

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!

like image 555
Peter Rankin Avatar asked Nov 02 '22 22:11

Peter Rankin


1 Answers

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.

like image 174
Raffaele Avatar answered Nov 15 '22 05:11

Raffaele