How to redirect and/or delay depending on referrer?

我是研究僧i 提交于 2019-12-12 02:52:56

问题


I want to allow traffic to my site only from a certain referring URL like "example.com/123". I want the rest of traffic to be redirected to the same referring URL after a particular delay, say 1 or 2 minutes. I want the traffic that comes from example.com/123 not to be referred anymore.

I thought of using something like this but I have no clue how to edit to meet my requirements:

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

回答1:


You'll need to have something in your php script that affect the header of the page, and not the header of the actual server response.

So in the part of the script that generates the header of your pages, you need something like this:

<!-- this is the header of your page -->
<head>
  <title>Your Title</title>
  <?php
    $referrer = $_SERVER['HTTP_REFERER'];

    // if referer isn't from example.com/123 we setup a redirect
    if ( !strstr($referrer, '://example.com/123') )
       print ('<META HTTP-EQUIV=Refresh CONTENT="60; URL=http://example.com/123">\n');

    ?>
  <!-- maybe some other stuff -->
</head>

So if the referer isn't from http://example.com/123, then this line will be inserted into the header:

<META HTTP-EQUIV=Refresh CONTENT="60; URL=http://example.com/123">

which tells the browser to redirect to the URL (in this case http://example.com/123) after 60 seconds.



来源:https://stackoverflow.com/questions/19229923/how-to-redirect-and-or-delay-depending-on-referrer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!