include header.inc.php based on HTTP_HOST

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
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

include header.inc.php based on HTTP_HOST

Post by markosjal »

I want to first thank some of you guys for the help lately. It is much appreciated.


Now I have to move on to a new step. I have 2 sites that both load a header.inc.php. In both cases these files are the top of the page with images and navigation. I believe these are called as include statements on both sites.

I have mydomain.com pointed there as well as sub.otherdomain.com

I want to modify the scripts to look for for the HTTP _HOST and if the host is sub.otherdomain.com, a different header.inc.php will be loaded such as XXheader.inc.php.

Of course is the host is www.mydomain or mydomain.com (or any other domain for that matter), I need to load the default header.inc.php

I have looked at several code samples and found them quite confusing. Sample code appreciated, and always helps with some explanation.


thanks for any help in advance
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: include header.inc.php based on HTTP_HOST

Post by Apollo »

Code: Select all

if ($_SERVER['HTTP_HOST']=='foo.domain.com')
{
  include('fooheader.inc.php');
}
elseif ($_SERVER['HTTP_HOST']=='bar.domain.com')
{
  include('barheader.inc.php');
}
else // use generic header.inc.php for any other (or no) subdomain
{
  include('header.inc.php');
}
You'll probably save yourself trouble if you would put this in 'anyheader.inc.php' for example, and just include('ahyheader.inc.php') in every php. This way if you ever add or change subdomains, you have to edit only anyheader.inc.php, rather than every php script that uses this.
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: include header.inc.php based on HTTP_HOST

Post by markosjal »

Seems to work beautifully!

Now I should also modify it for the correct favicon per domain!

Thanks
markosjal
Forum Commoner
Posts: 63
Joined: Fri Apr 16, 2010 10:15 pm

Re: include header.inc.php based on HTTP_HOST

Post by markosjal »

Apollo,

I used this code now in 2 different scripts, as well as a modified version to take care of a link problem as well with one of those scripts. Thank you so much! Today's lesson was more useful than I had imagined!

Still can not get it to work on my favicon issue however. I may be stuck with that for a while
Post Reply