Stop That Referrer in PHP!

Today I will show you a method to stop the traffic that a referrer site is sending your way, you may wonder why you would want to ignore traffic, after all, isn’t inbound traffic to your site fantastic no matter what?

The simple answer is “NO!”

Let me explain it in a real life situation so that you can better understand where to use this.

Understanding where to use it

You have a site that is using a web service or API of yours and it is sending loads of unwanted traffic your way, you actually want the service to be equally distributed but this one particular site is just overdoing it. So you want to stop all their traffic to you and only allow proper direct API requests or whatever the case.

Now for some code

At the very top of the php page that is being called – in our example we will call it “http://ataiva.com/api/" – you will add in the following PHP code:

if (strstr($_SERVER['HTTP_REFERER'],"theotherdomain.com")) {
    header("Location: http://theotherdomain.com");
}

Make sure to add this code before any other code or rendering objects, otherwise you will receive a PHP error!

Explaining the code

What this code will do: it will check to see if the visitor came from that site or was somehow referred from it and it will just send them back to that site’s homepage.

And bandwidth?

This technique does not get rid of the bandwidth used up entirely as the visitor still connects to your site, but it does not show the user anything as the php redirection header kicks in prior to any browser rendering. So as they click on the link at the original site, it immediately takes them to that site’s homepage. Very minimal bandwidth usage occurs during this period.