Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

cleo

macrumors 65816
Original poster
Jan 21, 2002
1,186
0
Tampa Bay Area, FL, USA
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?
 
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
}
 
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!";
}
?>
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.