user1114968
user1114968

Reputation: 87

HTTP_REFERER redirect php code not working?

The following script is in the header of my website. I want to use google analytics to monitor the amount of traffic that a specified website is sending to the website I am driving to. The script will redirect users from my website to the website I am driving traffic to. I tested the script out and it's not redirecting. I put a link to my website on another website I have to test the script out but I was not redirected to the website I am trying to drive traffic to. Any ideas?

 <?php
 $ref = $_SERVER["HTTP_REFERER"];
 if ( $ref != 'http://www.websitetrafficiscomingfrom.com' )
exit;
 ?>

 <?php if ( $ref == 'http://www.websitetrafficiscomingfrom.com' ) { ?>

 <script type="text/javascript">

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-32605120-1']);
_gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();

</script>

 <!-- <?php } else if ($_SERVER['HTTP_REFERER'] != "") { ?>

 <script type="text/javascript">
 var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-32554427-2']);
  _gaq.push(['_trackPageview']);

(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
   var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
 })();

 </script> -->

 <meta http-equiv='REFRESH" content='0;url=websitetrafficissupposedtogoto.com'>

 <?php } ?>

Upvotes: 0

Views: 692

Answers (1)

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324610

For starters, a referrer will always be either blank, or a full URL (including the trailing /, if needed). Therefore it will NEVER be the value you ask for. Consequently, you will ALWAYS die and result in a blank page that doesn't do anything.

Aside from that, I really have to wonder what you're doing. Your question doesn't seem to make any sense when compared to the code that accompanies it, and what code there is seems redundant. I mean, you check if a != b, exiting if so. Otherwise, check if a == b, as if there's some chance it might suddenly not be true anymore. To make matters worse, you have a commented-out else block, showing that at some point you clearly intended to yet again check if a != b, even though your code has already died if that's the case...

I would suggest dropping this code entirely. It's not useful or reliable to use HTTP_REFERER, the only thing I would use it for is as a feeble barrier to block most hotlinking attempts. Even then, only with caution.

Upvotes: 1

Related Questions