5秒后重定向,但只允许引荐来源网址访问该页面

Redirect after 5 seconds but only allow the page to be accessed by the referrer

本文关键字:访问 许引荐 重定向 5秒      更新时间:2023-09-26

我试图让页面 1.php 在 5 秒后重定向到 page2.php。但是,page2.php 需要是一个受限制的页面,只有在您从 --> mydomain.com/page1.php 发送邮件时才能查看,如果您在地址栏中手动键入地址,则无法访问。

我尝试过使用共享密钥,htaccess和php HTTP_REFERRER的方法。

我相信

问题来自重定向,我相信这是因为重定向脚本没有发送HTTP_REFERRER,因此 page2.php 正在查看从重定向脚本发送的 url 是手动输入的。我尝试过使用简单的php重定向和javascript。以下是我使用过的两种不同的重定向脚本。

PHP版本。

header( "refresh:5;url=page2.php" );

Javascript版本。

<script type="text/javascript">   
function Redirect() 
{  
    window.location="page2.php"; 
} 
setTimeout('Redirect()', 5000);   
</script>

我已经尝试过使用完整的 url 和带有/不带 http://例如 mydomain.com/page2.php。

第 2 页.php

只需要接受来自第 1 页.php的流量。我不反对如何实现这一目标。使用共享密钥或任何其他方面,只要用户无法手动输入地址并访问页面。我也完全知道推荐人可能会被欺骗,但我没有专业知识来达到高级水平。

您可以使用会话数据来确保第 2 页的用户已通过第 1 页

通过会话的工作方式,即使根本没有加密,encrypted string也非常安全。

在第 1 页上:

session_start();
$_SESSION['secret_key'] = 'encrypted_string';

在第 2 页上:

session_start();
if($_SESSION['secret_key'] == 'encrypted_string'){
   // user is authorized
   echo 'You are authorized to see this page';
}
else{
    echo 'Please visit page1 before accessing this page';
}
// Logic for authorized user

或者,第 2 页的较短版本:

if(empty($_SESSION['secret_key']) || $_SESSION['secret_key'] != 'encrypted_string'){
   die('You are not authorized to view this page.');
}
echo 'only authorized user will see from here forward';

顺便说一句,在测试时,请记住,设置会话后,您将不得不删除浏览器中的会话,或使用隐身再次测试。要删除 chrome 上的缓存,ctrl+shift+delete并选择 Cookie 和其他

这是我使用 3 页的方法。

着陆页上,包括您的 JavaScript,这会将您重定向到一个中间页面,该页面在重定向到最终页面之前设置会话变量。

在最后一页上,检查会话变量,确定是否显示页面,然后取消设置会话变量(因此,如果他们重试而不返回第一页,它将不再起作用)。

P1.php

<?php session_start(); ?>
<script type="text/javascript">   
function Redirect() 
{  
     window.location="p12.php"; 
} 
setTimeout('Redirect()', 5000);   
</script>

第12页.php

<?php session_start();
$_SESSION['secret'] = 'todays_password';
$newURL = 'p2.php';
header('Location: '.$newURL);
?>
<script type="text/javascript">   
function Redirect() 
{  
     window.location="p2.php"; 
} 
Redirect();   
</script>

第2.php 页

<?php session_start();
if (isset($_SESSION['secret']))
{
    if ($_SESSION['secret'] == 'todays_password')
    {
        //The user provided the correct secret session variable
        echo 'welcome. you can view this page.';
        //Put all of your page content here
        ?>
        <!-- HTML content should be in between php delimiters, like this-->
        <?php
    }
    else
    {
        //The user supplied a secret code, but it was not the correct one
        echo 'invalid secret.';
        //You can also add code for redirecting the user back to p1 here
        //Or just display an error message
    }
}
else
{
    //The user did not provide a secret session variable -- they most likely did not pass through p12.
    echo 'error, you are unable to view this page';
    //You can also add code for redirecting the user back to p1 here
    //Or just display an error message
}
unset($_SESSION['secret']); //This line makes the user return to p1 every time they visit p2 -- delete this line if you only want them to visit p1 once.
?>

为了使此方法安全,您需要为每个用户提供其机密会话变量的唯一值。将此变量以及用户访问 p1 时的时间戳存储为客户端的会话变量和服务器端数据库中。加载 p2 时,请检查它们提供的会话值在数据库中是否至少存在 5 秒。如果是,请让他们查看该页面。然后删除数据库中的值。