Home » Javascript » Redirect in function of the referer

Redirect in function of the referer

Posted by: admin July 12, 2018 Leave a comment

Questions:

I have this index.php to redirect the users to another pages in function of the referrer that he comes. If the user comes from a referrer that are not listed he should go to specific url.

All goes fine except when the user comes from any referer that not are listed. In this case, the site not make anything (in blank in the same domain). I not see where are the error.

<?php
if (isset($_POST["redirect"])) {
  $hash = $_POST["redirect"];

  if ($hash !== "") {
    $origin = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : "";
    $destination = "";

    if($origin == 'http://www.ads1.net/click/cc1/') {
      $destination = 'http://www.example1.com';
    } else if($origin == 'http://www.ads2.net/click/cc2/') {
      $destination = 'http://www.example2.com';
    } else if($origin == 'http://www.ads3.net/click/cc3/') {
      $destination = 'http://www.example3.com';
    } else if($origin == 'http://www.ads4.net/click/cc4/') {
      $destination = 'http://www.example4.com';
    } else if($origin == 'http://www.ads5.net/click/cc5/') {
      $destination = 'http://www.example5.com';
    } else {
      $destination = 'http://www.anothersite.com';
    }

    if($destination != "") {
      echo "<script>window.location.href = '".$destination."';</script>";
    }
  }
}
?>
Answers: