Solved

php referer http

Posted on 2010-11-30
3
310 Views
Last Modified: 2012-06-21
Can i use php referer to redirect visitors to my homepage if they have come from a different site or search engine... i.e. google?

Many thanks in advance
0
Comment
Question by:maccaj51
  • 2
3 Comments
 
LVL 7

Expert Comment

by:Sappbrosts
ID: 34240686
this will redirect all users, not sure this is what you're looking for

http://php.about.com/od/learnphp/ht/phpredirection.htm
0
 
LVL 7

Accepted Solution

by:
Sappbrosts earned 500 total points
ID: 34240709
maybe more like this:

Using PHP you can easily redirect your web site visitors to a different page depending on where they come from. For example you may have 2 different web sites selling a product but only one customer care web site where people submit support tickets. Depending from which of the 2 web sites a visitor comes to that support site you may want to show them different web page.

PHP stores information about the referring URL in one of its global variables - $_SERVER. You can easily access that value using the $_SERVER['HTTP_REFERER']. Now what you have to do is to see if any of the 2 domains is in that HTTP_REFERER variable. You can do this using he preg_match function.

This is the code that you can use:
<?
$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');
};
?>

Note, that because of using the header() function that code has to be put on the very top of your PHP page. Header redirect will throw an error if there is something sent to visitors browser before calling it.

Instead of using $_SERVER['HTTP_REFERER'] you can use $_SERVER['REMOTE_ADDR'] which carries information about visitors IP address. This way you can redirect users based on their IP address. If you detected that someone is trying to hack your web site you can use that PHP redirect to send him to a “Thank you page”

<?
$visitor = $_SERVER['REMOTE_ADDR'];
if (preg_match("/192.168.0.1/",$visitor)) {
      header('Location: http://www.yoursite.com/thank-you.html');
} else {
      header('Location: http://www.yoursite.com/home-page.html');
};
?>
0
 

Author Closing Comment

by:maccaj51
ID: 34515722
Brilliant!
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question