Get current URL from externally hosted include

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oscardog
Forum Contributor
Posts: 245
Joined: Thu Oct 23, 2008 4:43 pm

Get current URL from externally hosted include

Post 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 :)
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: Get current URL from externally hosted include

Post 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.
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: Get current URL from externally hosted include

Post 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
Post Reply