![]()
Aside from detecting visitors by their IP address, which I have covered in a previous post, you may also want to be able to detect what the referring website that you visitor has come from is, and react accordingly.
I find this particularly useful when people are add a link to one of my websites in their eBay listings claiming some form of affiliation between the product they are selling and the contents of my site – often completely illegitimately.
With a detect and redirect in place, any poor punter who looks at the spurious seller’s auction and chooses to click the link will find themselves on my website (regardless of which page on the website they had been originally linked to), having been automatically redirected to a big bold message about scams on eBay. Clever no?
Fortunately it’s very easy to do with a little bit of PHP in your header file (or at the top of each and every page on your site):
< ?php
$referrer = $_SERVER['HTTP_REFERER'];
if (preg_match("/ebay/",$referrer)) {
header("location: http://www.example.com/big-ebay-scam-warning");
exit();
};
?>
In the example above replace “/ebay/” with any string that would appear in the referring website that you want to detect, and obviously replace the header location with the page you want to send them to (or replace it with another function, whatever you’d like to do or show to those particular visitors).
Don’t get caught in a loop.
Just one final word of wisdom about this: if you’re redirecting to a particular page within your own site, be very careful that you don’t end up spinning your visitor out in an endless redirection loop. Create an exception for that particular page so that it doesn’t attempt to redirect your visitor to itself. After all, if they’re trying to get to that page, you’ve already got them where you wanted them anyway…!
No Responses to “Detect Visitors By Referring Address”