PHP Referrer Redirects

Status
Not open for further replies.

kjb1891

New member
Mar 12, 2007
216
1
0
I was wanting to have a page where if a visitor comes from a certain referrer it would redirect to another site, but if the visitor comes from any other referrer the page would load as normal. The one caveat though is that if I redirect a visitor I want to strip the referrer info when redirecting them.

Does a PHP header redirect(as below) strip referrer info to the site it's being redirected to or would I have to do some other kind of redirect in order to do that? Meta redirect maybe? Can you even use a meta redirect in a PHP script?

PHP:
<?php
$referrer=$_SERVER[‘HTTP_REFERER’];
if (preg_match("example.com/",$referrer))
header('Location: http://www.example2.com/');
?>
 


An easier approach may be the mod_rewrite capability of Apache (if you have it, into an .htaccess file on the site).

Code:
  [FONT=verdana][COLOR=White][SIZE=2]RewriteEngine On 
RewriteCond %{HTTP_REFERER} . 
RewriteCond %{HTTP_REFERER} (badsite\.com¦otherbadsite\.com) [NC] 
RewriteRule . http://%1%2 [R=301,L] 
[/SIZE][/COLOR][/FONT]


Basically the two lines basically means
if Refferer is blank OR if referer matches one of these sites (use \ in front of period, and seperate each domain by a | )

If you only wish to redirect specific domains and leave the rest (even blank) continuing onto your site, just remove the second line.

As for PHP, I think you need more than just the domain to work in a preg_match function (ie: something more suitable as regular expression). You could probably adapt that "
(badsite\.com¦otherbadsite\.com)" from the .htaccess code above since that stuff is regex.

If I find something PHP specific I'll let you know but try the condition formating above inside of your PHP to see if that works too. Also instead of a just Location try the following:

Code:
header("HTTP/1.1 301 Moved Permanently");
just before the header("Location... line.
 
Here's a PHP version I found based on the usage of preg_match (I'm not too familiar with the preg_match function as such I googled for an existing script).

Code:
<?
  $referrer = $_SERVER['HTTP_REFERER'];
  if (preg_match("/site1.com/",$referrer)) {
        header('Location:  http://www.customercare.com/page-site1.html');
  } elseif (preg_match("/site2.com/",$referrer)) {
        header('Location:  http://www.customercare.com/page-site2.html');
  } else {
        header('Location:  http://www.customercare.com/home-page.html');
  };
 
I was wanting to have a page where if a visitor comes from a certain referrer it would redirect to another site, but if the visitor comes from any other referrer the page would load as normal. The one caveat though is that if I redirect a visitor I want to strip the referrer info when redirecting them.

Does a PHP header redirect(as below) strip referrer info to the site it's being redirected to or would I have to do some other kind of redirect in order to do that? Meta redirect maybe? Can you even use a meta redirect in a PHP script?

PHP:
<?php
$referrer=$_SERVER[‘HTTP_REFERER’];
if (preg_match("example.com/",$referrer))
header('Location: http://www.example2.com/');
?>

Using the header() function in php does not strip the referrer. You must use a meta refresh redirect to strip the referrer. I experimented with this just a few says ago. Put your test code on a couple sites you own and print the referrer to the screen so you can see exactly where the referrer is carried through and where it is stripped.
 
Using the header() function in php does not strip the referrer. You must use a meta refresh redirect to strip the referrer. I experimented with this just a few says ago. Put your test code on a couple sites you own and print the referrer to the screen so you can see exactly where the referrer is carried through and where it is stripped.
a meta redirect would certainly strip it because the browser would then use the newly loaded page as the most recent refferer (I've found its nearly impossible to get a browser to spit up a website prior to the one that lead to the new page, least without inspecting cookies).

But I guess I could probably test header("location...") method to see how certain browsers treat that. Btw why would someone want to strip the refferer other than to act as a privacy proxy?
 
Hey thanks for the help guys. I knew that a regular HTML meta redirect would strip the referrer, but is there a way to do a meta redirect in PHP at all? I have pretty limited experience with PHP. So, I wasn't sure.
 
Hey thanks for the help guys. I knew that a regular HTML meta redirect would strip the referrer, but is there a way to do a meta redirect in PHP at all? I have pretty limited experience with PHP. So, I wasn't sure.

I made two PHP files on my server to test this.

re1.php
PHP:
<?
header("HTTP/1.1 301 Moved Permanently");
header('Location:  http://www.karlblessing.com/re2.php');
?>

re2.php
PHP:
<?
echo "You came from: ".$_SERVER['HTTP_REFERER'];
?>

Here's a Link to re1.php (that'll redirect to re2 and tell your referer)
http://www.karlblessing.com/re1.php

Here is a link to re2.php to compare redirect to direct link
http://www.karlblessing.com/re2.php
 
I guess I could do a double redirect by using the PHP header redirect to another page on my site that then just does a simple HTML meta redirect to the end destination site.

You would think that there would have to be some way to redirect using PHP that would strip referrer info though.
 
I guess I could do a double redirect by using the PHP header redirect to another page on my site that then just does a simple HTML meta redirect to the end destination site.

You would think that there would have to be some way to redirect using PHP that would strip referrer info though.

I think the idea behind a 'redirect' though is to redirect original traffic to a new destination as opposed to doing anything to that connection.

But as you said perhaps a landing page that would be like "please wait 3 seconds, as we redirect you to your new location" or something like that would suffice. You wouldn't even have to do the first redirect in a manner of speaking if you did that.
 
I could just disable my browser from sending referrers and your script is worthless.

Don't rely on referrers for *anything* because they can be spoofed or not there at all.
 
Hey thanks for the help guys. I knew that a regular HTML meta redirect would strip the referrer, but is there a way to do a meta redirect in PHP at all? I have pretty limited experience with PHP. So, I wasn't sure.


Code:
echo "<meta http-equiv=\"refresh\" content=\"2 url=http://webname.com\">";
Thats meta refresh, PHP style :)
 
Karl-

I need to learn how to take pictures. You have some hot fucking whores on your site. Jesus christ. They take and pose too. I'd take some of them and pose them! W00t!

enigmabomb
 
Idea

Hi all!

can any one of you help me out to find out about how to redirect a web sites i have found it a interesting charm to do can any one help?
 
Status
Not open for further replies.