Hi, simple question.. I have 10 sub-domains pointing to my index.php file.. Question: How can I print the keyword of the sub.domain on page? ( example: keyword.domain.com ) Thanks for help, Brian
Use explode() $peices = explode('.', $_SERVER['HTTP_HOST']); echo $peices[0]; will print out the keyword of the sub domain.
Hey There, dannywwww is correct, explode will do the job perfectly. I've made it into a function, to check the HTTP_HOST is set and then split the sub-domain out of it into an array - It will check the element of the array before returning it, else an error. echo 'Current Sub-Domain is:' . getSubDomain(); function getSubDomain() { if(isset($_SERVER['HTTP_HOST'])) { $subDomain = Array(); $subDomain = explode('.', $_SERVER['HTTP_HOST']); if(isset($subDomain[0])) { return $subDomain[0]; } else { return 'Failed getting sub-domain for ' . $_SERVER['HTTP_HOST']; } } } PHP: Regards, Steve
Only problem is if someone adds the www before the subdomain: www.subdomain.domain.com will return "www" Here's a better function: function subDomain($domain) { $host = $_SERVER['HTTP_HOST']; $parts = explode('.',$host); $where = array_search($domain,$parts); $sub = $parts[$where-1]; if ($sub == 'www' || $sub == '') { return('No sub-domain'); } return ($sub); } $domain = "YOURDOMAIN.COM"; echo "Subdomain is " . subDomain($domain); PHP: