|
||||||||||||||
|
||||||||||||||
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:
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”
| Comments |
| Monty posted on July 8, 2010, 12:44 pm |
| Many thanks for this code!! You have just made my life so much easier :) |
| Chris Bee posted on May 27, 2010, 12:31 pm |
| Thanks for that..... |
| salma posted on January 18, 2010, 12:43 pm |
| Hi i need a help, Suppose i open xyz page. it has to be directed to html page with a msg and url for new page for xyz. How can this be done. |
| ganool posted on December 7, 2009, 1:18 pm |
| nice idea bro.. its work..and very help full for me |
| Farooque posted on July 22, 2009, 7:09 am |
| IT WORKED PERFECTLY.... THANKS |
| Steve posted on July 16, 2009, 5:39 am |
| I am a bit of a novice when it comes to preparing coding. What I know is that I\'m looking for a basic HTML code that will allow the following: a) redirect b) show an \"assigned referring page\" c) it must be supported by Internet Explorer & Firefox EG. redirect to a site, BUT the referring page goes to a specified page on my own personal site http://www.xyzxyx.com/search.php?what=crabtree this \"landing page\" is what I want to been seen as the REFERED page as it is the landing page for driving sales. Not sure if this is clear enough....thanks in advance for you time and thoughts. Cheers! Steve |
| Asela posted on April 8, 2008, 12:41 pm |
| This is very useful, mind if i asking you guys, is it possible to add multiple IP adresses (192.168.0.1) to this code. <? $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'); }; ?> |
| oli posted on September 26, 2007, 1:57 am |
| Would it be possible to pull out a users location from os language/browser versions and import it into this script to choose the reffering page by location (Ie en-GB, en-US) This would be unbelievably usefull to me! |
| bontb posted on August 30, 2007, 6:38 pm |
| This helped me a lot , I am now redirecting, but I had to remove last statement \"else header location mydomaiin |
| Chris posted on July 4, 2007, 3:20 am |
| this looks like what I am trying to do.. however is there a way to set a time delay, i.e. "you will be redirected in 5 seconds". Veselin: in order to have a message on your webpage showing "you will be redirected in 5 sec" you will need to use JavaScript |