This is really handy for those of us who have the same code handling multiple sites or multiple sub-domains.
A case in point: When I coded NetBoardz (my free forum hosting service now defunct), I had one codebase handling all 250 forums. How? Simple. When the code runs, it determines which site the user is loading and does different things (like using different databases) dynamically.
How to determine the domain the user is using to view your site:
$domain = $_SERVER['HTTP_HOST'];
if ($domain == "xyz") {...
} else if ($domain == "uvw") {...
}
In the example above you can see that we have put the domain that the user has used to view your site into the $domain variable, loading the value from the PHP global variable, $_SERVER. The $_SERVER variable is global, which means you can access it anytime and anywhere in your code.
For more information on PHP’s predefined global variables to see what’s available to you, click here.
How to determine the sub-domain the user is using to view your site:
Sample code is from NetBoardz, which is based off of phpBB 2:
$subdomain = strpos($_SERVER['HTTP_HOST'], "."); $subdomain = substr($_SERVER['HTTP_HOST'], 0, $subdomain);
$dbname = "nb_".$subdomain;
mysql_select_db($dbname, $sql_link);
Here you can see that we retreived the whole hostname, including the top-level domain and subdomain, then used the PHP functions strpos and substr to take anything before the first dot. For example, the whole hostname “testforum.netboardz.com” passed through this code would end up as “testforum”.
After, we use that subdomain name to calculate which forum database to load. Of course, once you have the domain or subdomain in a variable, you are able to handle your code as you wish!
I hope this small tid-bit of code helps you out in some way. I know that there are tons of these snippets and tips littered across the internet and while I was learning PHP they were really handy. I’ll post more over time, always in this same category.

[...] Technorati Search for: databases wrote an interesting post today onHere’s a quick excerpt This is really handy for those of us who have the same code handling multiple sites or multiple sub-domains. A case in point: When I coded NetBoardz (my free forum hosting service now defunct), I had one codebase handling all 250 forums. How? Simple. When the code runs, it determines which site the user is loading and does different things (like using different databases) dynamically. How to determine the domain the user is using to view your site: $domain = $_SERVER[’HTTP_HOST’]; if ($domain [...]
[...] Excerpted from:PHP Tricks: How To Handle Multiple Domains [...]
[...] reading John Rockefeller’s post on Handling multiple domains and less recently Richard Heye’s post on displaying errors, I thought I’d write a [...]
[...] Rockefeller has a tip he’d like to share with all of the other PHP developers out there – a little method [...]
[...] I’m back again. Today first post is about great tip how you can handle multiple domains in your web applications. A case in point: When I coded [...]