Zend - The PHP Company




HTTP

Add Code


grab_domain  

Type: code fragment
Added by: amiller
Entered: 24/05/2001
Last modified: 05/12/2000
Rating: - (fewer than 3 votes)
Views: 5312
I had one web site with over 30 different domain names pointed to it and I needed to write a little fragment that would return a variable giving me the Fully qualified domain name someone came to my site with to determine some basic page elements and a database query. This is not perfect (see note below), but it did do the job for me. The problem was that people could reach the site using different variations of the differing domain names such as http://domain.com and http://www.domain.com . I looked around and found a little used PHP constant and expanded it a bit and came up with the following. I also noticed there were others out there trying to accomplish the same thing using the HTTP_REFERER constant, however, that only works if the person coming to your site was referred there, this will cover those who type in your domain name as well as those that are referred. With the returned variable from this function you can customize your site however you like with an accompaning statement to define your values. NOTE: This only works if the host name in question is either http://domain.com or http://www.domain.com I would like to expand this function to allow for sub domain's such as http://subdomain.domain.com . As it stands it will only strip out "www" if it exists. If there is someone out there who could write a fancy regular expression which would accomplish the task so the function returned only the Fully Qualified Domain Name I would be forever greatful! CHANGES: 1.0 to 1.1 global $HTTP_HOST variable is now pulled from within the function.


<?php

function grab_domain()
{
        global 
$HTTP_HOST;
        
$str $HTTP_HOST;
        
$str trim($str);
        
$str strtolower($str);
        
$str str_replace("www." """$str");
        
        return 
"$str";
}
        
?> 


Usage Example


<?php 

$domain 
grab_domain();

if (
$domain == "domain1.com") {
    include
"page_defs_domain1.inc";
    }
if (
$domain == "domain2.com") {
    include
"page_defs_domain2.inc";
    }

?>

You can get as creative as you like,
in my case I wrote a switch statement
that called the correct page definition
include file for each domain.


Rate This Script





Search



This Category All Categories