Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php referer http

Posted on 2010-11-30
3
Medium Priority
?
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses how to create an extensible mechanism for linked drop downs.
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.
Suggested Courses

618 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