Why should I rely on PHPJabbers.com for all my PHP scripting needs? What do you offer me when buying some script?
  • Top quality scripts
  • Full demo before buying
  • Best pricing
  • Script customization
  • 7 days a week support
  • Installation support
  • Secure buying
  • Redirect based on referrer or IP address

    posted on 2006-05-17 17:06:32

    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');
    };
    ?>

    Comments
    Asela posted on 2008-04-08 12:41:13
    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 2007-09-26 01:57:42
    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 2007-08-30 18:38:05
    This helped me a lot , I am now redirecting, but I had to remove last statement \"else header location mydomaiin

    Chris posted on 2007-07-04 03:20:34
    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

    Jason DAngelo posted on 2007-02-10 10:01:31
    do not forget... some websites translate you hosts IP as the referrer... thus the /123.123.123.123/ as the referrer

    Jason DAngelo posted on 2007-02-10 09:59:15
    I would try adding the other website referrers...
    http://mydomain.com
    http://www.mydomain.com
    http://mydomain.com/
    http://www.mydomain.com/

    There are many formats... See if your referrer is getting od code... show your referrer as part of your page, to debug.

    print ($referrer)

    on your redirected page... or as your page...

    Til posted on 2006-12-08 11:31:55
    Hi!
    First, thanks a lot for this code! Now I have tried this code in my index.php on my homepage (in a subdirectory) and it works well with Firefox or Opera, but not with Internet Explorer! Using the
    Internet Explorer the user is always redirected, even when he comes (is referred) from mydomain.com. Any idea how to correct this? Thanks in advance!
    Mrs.Bungle.

    here's my index.php:

    <?
    $referrer = $_SERVER['HTTP_REFERER'];
    if (preg_match("/mydomain.com/",$referrer)) {
    include("iindex.htm");
    } else {
    header('Location: http://mydomain.com');
    };
    ?>

    -----------------
    PHPjabbers:
    try using apache_request_headers() instead (formerly getallheaders).
    <?php
    $host=apache_request_headers();
    if(!eregi('domain.com',$host[Referer])){
    // TRUE
    }else{
    // FALSE
    }
    ?>

    bhaskar verma posted on 2006-08-18 02:33:48
    hello, ur logic of referer has helped me a lot.
    Thnx ..

    Pages:   1
       
    If you have any additions, suggestions or modifications for this example please send an email to or use the form below.
       
    Your name:
    Your email: (email address will not be posted on the web site)
    Comment:
    Verification