1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Find Referring Domain (without www and any other sub domain) instead of Full path URL

Discussion in 'PHP' started by newgenservices, Aug 25, 2008.

  1. #1
    Hello,
    I know that the following code prints output of the referring URL.

    
    <?php
    $ref=$_SERVER['HTTP_REFERER'];
    echo "$ref"; 
    ?>
    Code (markup):
    For example the referring URL is http://forums.digitalpoint.com/showthread.php?t=123, i need the value to be grabbed as just digitalpoint removing all the stuff after and before.

    The following links may be useful in reaching the solution for this.

    http://phosphorusandlime.blogspot.com/2007/08/php-get-base-domain.html
    http://in.php.net/parse_url

    PHP str_replace and other PHP Standard functions. I am trying to get this working by preg_replace and str_replace but really couldn't get it working.

    Expecting a quick reply from someone who had achieved such a thing.
     
    newgenservices, Aug 25, 2008 IP
  2. AimHigher

    AimHigher Member

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #2
    This'll work:

    
    <?
    
    if (isset($_SERVER['HTTP_REFERER'])) { // check there was a referrer
    
    	$uri = parse_url($_SERVER['HTTP_REFERER']); // use the parse_url() function to create an array containing information about the domain
    	Echo $uri['host']; // echo the host
    
    }
    
    ?>
    
    Code (markup):
     
    AimHigher, Aug 25, 2008 IP
  3. abcvps

    abcvps Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    AimHigher is correct... this will work as you want it to be...
     
    abcvps, Aug 25, 2008 IP
  4. newgenservices

    newgenservices Well-Known Member

    Messages:
    862
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    105
    Digital Goods:
    1
    #4
    Thanks for this. I tried this but I just need something more.

    say for example, the referring domain may be

    domain.com or www.domain.com

    I would like the answer to be domain.com irrespective of that.

    So is it possible to match and split ?
     
    newgenservices, Aug 25, 2008 IP
  5. AimHigher

    AimHigher Member

    Messages:
    40
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #5
    
    <?
    
    if (isset($_SERVER['HTTP_REFERER'])) {
    
    	$uri = parse_url($_SERVER['HTTP_REFERER']);
    	$domain = substr($uri['host'], strpos($uri['host'], ".")+1, strlen($uri['host']));
    	Echo $domain;
    
    }
    
    ?>
    
    Code (markup):
    If the referrer is something like subdomain.domain.com it will strip the subdomain though.

    Here's what you could do if you want to strip www. but keep the subdomains:

    
    <?
    
    if (isset($_SERVER['HTTP_REFERER'])) {
    
    	$uri = parse_url($_SERVER['HTTP_REFERER']);
    	$domain = str_replace("www.", "", strtolower($uri['host']));
    	Echo $domain;
    
    }
    
    ?>
    
    Code (markup):
     
    AimHigher, Aug 26, 2008 IP