重定向在referer的功能

时间:2018-05-23 00:30:30

标签: javascript php html

我有这个index.php将用户重定向到他来的推荐人的另一个页面。如果用户来自未列出的引荐来源,则应转到特定网址。

一切顺利,除非用户来自未列出的任何引用者。在这种情况下,网站不做任何事情(在同一个域中为空)。我没看到错误在哪里。

<?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>";
    }
  }
}
?>

1 个答案:

答案 0 :(得分:0)

尝试将代码更改为此。如果未设置$_POST['redirect'],则会重定向到http://www.anothersite.com。否则,如果$_POST['redirect']不为空,则会根据$_SERVER['HTTP_REFERER']重定向。用户不会被重定向的唯一情况是设置$_POST['redirect']但是为空(即$_POST['redirect'] === '');我在那里的else块中留下了评论,你也可以决定从那里重定向?

$destination = "";
if (isset($_POST["redirect"])) {
  $hash = $_POST["redirect"];

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

    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';
    }
  }
  else {
    // $_POST['redirect'] is set but empty (=== '')
    // if you want to redirect for this case, add a URL here as well e.g.
    // $destination = 'http://www.anothersite.com'
   }
}
else {
  $destination = 'http://www.anothersite.com';
}
if($destination != "") {
  echo "<script>window.location.href = '".$destination."';</script>";
}