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.

Regex to get hostname from referer

Discussion in 'PHP' started by phree_radical, Nov 19, 2006.

  1. #1
    I'm pretty new to regex, and this is the first time I've tried to use them in php. I'm just trying to take $_SERVER['HTTP_REFERER'] and produce a host string such as http://forums.digitalpoint.com. I'm not even sure if I'm using the best function p;

    eregi('^(.*//[a-z0-9.-:]+)',$_SERVER['HTTP_REFERER'],$adcustom['referring_host']);
    var_dump($adcustom['referring_host']);
    PHP:
    It seems like this would work as I didn't didn't ask for more slashes within the [] brackets, but it still returns the rest of the referer string that includes the filename.
     
    phree_radical, Nov 19, 2006 IP
  2. coolsaint

    coolsaint Banned

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    This is the example of php.net

    <?php
    // get host name from URL
    preg_match('@^(?:[url]http://)?([/url][^/]+)@i',
       "http://www.php.net/index.html", $matches);
    $host = $matches[1];
    
    // get last two segments of host name
    preg_match('/[^.]+\.[^.]+$/', $host, $matches);
    echo "domain name is: {$matches[0]}\n";
    ?> 
    PHP:
    Put the referrer variable in place and you will get referring host in return.

    Wish you good luck.

    Ref. http://au3.php.net/preg-match
     
    coolsaint, Nov 19, 2006 IP
  3. phree_radical

    phree_radical Peon

    Messages:
    563
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #3
    var_dump(preg_match('@^(?:[url]http://)?([/url][^/]+)@i',$_SERVER['HTTP_REFERER'], $matches));
    Code (markup):
    prints int(0) when $_SERVER['HTTP_REFERER']==='http://localhost:82/labs/test.php'

    But I am still trying to figure it out

    Thanks coolsaint :)
     
    phree_radical, Nov 19, 2006 IP
  4. streety

    streety Peon

    Messages:
    321
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #4
    This is the code I used for a different project where I wanted to get the root domain

    $url = preg_replace("/^http:\/\//", "", $_GET['url']);
    	
    $url_tokens = explode("/", $url);
    $url = $url_tokens[0];
    PHP:
    First I stripped off the http://, then seperated out the root and each folder and then just selected the root. It's not pure regex but it works and it was easy to create.
     
    streety, Nov 19, 2006 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    
    
    extract(parse_url($_SERVER['HTTP_REFERER']));
    
    echo $scheme .'://'. $host;
    
    PHP:
     
    nico_swd, Nov 19, 2006 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    Also, if you want to block a certain referer, you should remove the www. when checking.

    
    
    extract(parse_url($_SERVER['HTTP_REFERER']));
    
    $referer = $scheme .'://'. str_replace('www.', null, $host);
    
    echo $referer;
    
    PHP:
     
    nico_swd, Nov 19, 2006 IP
  7. phree_radical

    phree_radical Peon

    Messages:
    563
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks. I'm gonna put off this regex until after I launch this project. p;
     
    phree_radical, Nov 19, 2006 IP
  8. FunkyRes

    FunkyRes Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Old post - but I see the regex solution to this problem everywhere.
    Just use explode with / as the delimiter.

    
    $foo = @$_SERVER['HTTP_REFERER'];
    if ( strlen($foo) > 0 ) {
      $bar = explode("/",$foo);
      $host = $bar[2];
      } else {
      $host = "";
      }
    
    Code (markup):
     
    FunkyRes, Dec 19, 2008 IP
  9. herbacious

    herbacious Peon

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    why regex when php has the parse_url function?
     
    herbacious, Dec 19, 2008 IP
  10. FunkyRes

    FunkyRes Guest

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    heh - even easier :)
     
    FunkyRes, Dec 19, 2008 IP