PHP: show only if user came from external site

Discussion in 'Web Design and Development' started by cleo, Oct 21, 2008.

  1. cleo macrumors 65816

    cleo

    Joined:
    Jan 21, 2002
    Location:
    Tampa Bay Area, FL, USA
    #1
    I'd like to have a chunk of content on my pages display when people come to the page from an external site (a search engine, blog, whatev); or, in other words, hide the content if the person reached the page from another page on my site.

    I know this has something to do with $_SERVER['HTTP_REFERER'], but I can't figure out exactly how to construct the conditional.

    Any tips?
     
  2. angelwatt Moderator emeritus

    angelwatt

    Joined:
    Aug 16, 2005
    Location:
    USA
    #2
    This should work, though untested.
    PHP:
    if (!preg_match('/yoursite.com/'$_SERVER['HTTP_REFERER'])) {
      
    // Make it here if previous page was not from this site
    }
     
  3. jeremy.king macrumors 603

    jeremy.king

    Joined:
    Jul 23, 2002
    Location:
    Fuquay Varina, NC
    #3
    Because REFERRER isn't very reliable, you may want to consider passing something via query string from links on your own site and check for the presence of that.

    Otherwise, you can try a pattern match on referrer
    Code:
    <?php
    if (preg_match("/yourdomain.com/i",$_SERVER['HTTP_REFERER'])) {
        echo "OMG! You totally came from my site!";
    }
    ?>
    
     

Share This Page