Page 1 of 1

Get current URL from externally hosted include

Posted: Thu Jun 16, 2011 8:20 am
by oscardog
Hey,

I'm sure this is simple enough, I just can't work it out.

I have a ton of websites with the same menu so rather than having loads of includes I have one which powers the menu on all the sites. I need to get the current page URL from that file, but it just returns the path relative to the include.

So, for example, I have a file at example-external.com/inc/myfile.php. That file is included at example.com/index.php. The code must go in myfile.php and it should return example.com/index.php.

However, no matter what magic constant (tried __FILE__) or $_SERVER variables... Nothing. Maybe it's not possible, any ideas?

Thanks in advance :)

Re: Get current URL from externally hosted include

Posted: Thu Jun 16, 2011 11:20 am
by Apollo
Can't be trusted 100% (it depends on the client to specify this) but try this:

Code: Select all

$_SERVER['HTTP_REFERER']
By the way, you cannot include php that is stored on server A, from a php script on server B. When you call/load/include something external, like example-external.com/yourscript.php then example-external.com's apache / PHP parser will execute the PHP script locally (that is, at example-external.com) and just output the result.

Remember: PHP is parsed server side. Keep in mind what is considered to be server side, and what is client side, in your situation.

Re: Get current URL from externally hosted include

Posted: Thu Jun 16, 2011 11:57 am
by twinedev
$_SERVER['HTTP_HOST'] will give you the host of the site being called.

If you don't have the site set for SEO to force example.com to www.example.com (or vice versa), make sure you check for both.

-Greg