Page 1 of 1

include header.inc.php based on HTTP_HOST

Posted: Sun May 23, 2010 12:15 am
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

Re: include header.inc.php based on HTTP_HOST

Posted: Sun May 23, 2010 12:56 am
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.

Re: include header.inc.php based on HTTP_HOST

Posted: Sun May 23, 2010 4:32 am
by markosjal
Seems to work beautifully!

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

Thanks

Re: include header.inc.php based on HTTP_HOST

Posted: Sun May 23, 2010 11:39 pm
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