1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

[GET]Referer based redirect.. option to redirect xx% of the traffic

Discussion in 'HTML & JavaScript' started by hazard_mkd, Aug 13, 2011.

  1. hazard_mkd

    hazard_mkd Junior Member

    Joined:
    Oct 1, 2009
    Messages:
    197
    Likes Received:
    58
    The following example will redirect 10% of the traffic that comes from google youtube and some other referer.. you can specify as many referers as u like, keeping with the syntax of the code..

    Code:
    <?php
    $rand_num = rand(1, 10);
    if($rand_num == 3){
    $referer = str_replace("http://", "", str_replace("www.", "", $_SERVER['HTTP_REFERER']));
    $referer = substr($referer, 0, (strpos($referer, "/") === FALSE ? strlen($referer) : strpos($referer, "/")));
    $fromURL = array(google.com","youtube.com","referesite3.com");
    if(!in_array($referer,$fromURL)) {
      echo '<meta http-equiv="refresh" content="0;url=DESTINATION URL">';    
        exit;    
    }
    }
    ?>
    the next one will redirect 33% .. it also uses another method of redirection.

    Code:
    <?php
    $rand_num = rand(1, 3);
    if($rand_num == 3){
    $referer = str_replace("http://", "", str_replace("www.", "", $_SERVER['HTTP_REFERER']));
    $referer = substr($referer, 0, (strpos($referer, "/") === FALSE ? strlen($referer) : strpos($referer, "/")));
    $fromURL = array("google.com","facebook.com","youtube.com");
    if(!in_array($referer,$fromURL)) {
      header("Location: DESTINATION URL");
      exit;
    }
    }
    ?>
    modify it to suit ur needs.. ;)

    the script is made to work with different referers from the same domain.. you just need the domain name.. so for example if visitor comes from facebook . com/aoishfasf/aoishdoasd/oiashdoasd .. the visitor will still be redirected




    EDIT: sorry for wrong category.. mods please move this to PHP & Perl ..