views:

47

answers:

2

I'm going to develop a website in PHP. But not sure if the method i'm going to use is the best approach. There will be many addon domains for the same site. But content will be filtered based on the domain used to visit the site.

For Example If a user comes from the domain siteusa.com then the content will be shown filtered accordingly specific user. If the user comes from siteuk.com/sitechina.com the content will be filetered accordingly etc...

I'm planning to do something like this to detect the url and serve content

 $ref = getenv("HTTP_REFERER");
    echo $ref; 

or by

$host = $_SERVER['HTTP_HOST'];
echo $host ;

Is this is the best method to do this? Is there any possible bottleneck I may get into? This should not fail to detect the domain as its critical.

The main domain of the site will be serving unfiltered content and each addon domain will filter it according to filter set for each domain from backend.

There is a smilar question but in regard with codeigniter here.

+2  A: 

HTTP_REFERER is a bad choice. HTTP_REFERER will be empty when user visits your site directly.

HTTP_HOST/SERVER_NAME should do the trick but it can fail behind load balancing software or proxy.

So, it depends on your servers configuration. If you have access to hosts configuration (apache virtualhosts, for example), you can simply specify ENV variable in each VirtualHost for each domain.

Māris Kiseļovs
How will it be if it's hosted on a shared host or a reseller package?
esafwan
Depends. You can start with test what is returned by print $_SERVER['HTTP_HOST'];print $_SERVER['SERVER_NAME'];
Māris Kiseļovs
esafwan
If it's returning correctly, then you can rely on these variables and it will work while your hosting provider will not totally change hosting configuration. Load balancing - http://en.wikipedia.org/wiki/Load_balancing_(computing)
Māris Kiseļovs
does this have any difference if I'm developing in codeigniter?
esafwan
A: 
$http_referer = $_SERVER['HTTP_REFERER'];

if (strpos($http_referer, 'siteuk.com') !== FALSE || strpos($http_referer, 'sitechina.com') !== FALSE) {
    // code for users from siteuk.com or sitechina.com
}
else if (strpos($http_referer, 'siteusa.com') !== FALSE) {
    // code for USA visitors
}
else {
    // default code for users from neither of the above domains, or HTTP_REFERER not tracked
}

^ That's the best you can do really - provide a default set of content for if HTTP_REFERER isn't available.

EDIT: See comment below, I misread the question. Ignore this answer completely :)

chigley
Awful advice. HTTP_REFERER is the most unreliable header for this job.
Māris Kiseļovs
Think I've misread the question. I assumed that siteusa.com sitechina.com etc. were **external sites** linking to the OP's site. However after a second read it looks like the OP is hosting all of the above domains with one PHP application, and wants to determine where they're accessing from. My bad!
chigley