Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/react-native/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 5秒后重定向,但仅允许推荐人访问页面_Javascript_Php_.htaccess_Redirect - Fatal编程技术网

Javascript 5秒后重定向,但仅允许推荐人访问页面

Javascript 5秒后重定向,但仅允许推荐人访问页面,javascript,php,.htaccess,redirect,Javascript,Php,.htaccess,Redirect,我试图在5秒钟后将page1.php重定向到page2.php。但是,page2.php需要是一个受限页面,只有从-->mydomain.com/page1.php发送时才能查看该页面,如果在地址栏中手动键入地址,则无法访问该页面 我尝试过使用共享密钥、htaccess和php HTTP_referer的方法 我认为问题来自重定向,我认为这是因为重定向脚本没有发送HTTP_referer,因此page2.php将重定向脚本发送的url视为手动输入。我尝试了一个简单的php重定向和javascri

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

我尝试过使用共享密钥、htaccess和php HTTP_referer的方法

我认为问题来自重定向,我认为这是因为重定向脚本没有发送HTTP_referer,因此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>

函数重定向()
{  
window.location=“page2.php”;
} 
setTimeout('Redirect()',5000);
我用完整的url和带/不带http://例如mydomain.com/page2.php尝试了这些方法


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

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

按照会话的工作方式,
加密字符串
是非常安全的,即使它根本没有加密

第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';
顺便说一句,在测试时,请记住,一旦设置了会话,就必须在浏览器中删除会话,或者使用incognito再次测试。
要删除chrome上的缓存,请按住ctrl+shift+delete键,然后选择cookies和other(cookies和other),我将使用3个页面

在登录页面上,包括JavaScript,这将重定向到中间页面,该页面在重定向到最终页面之前设置会话变量

在最后一页上,检查会话变量,确定是否显示该页,然后取消设置会话变量(因此,如果他们在不返回第一页的情况下重试,则会话变量将不再工作)

p1.php

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

函数重定向()
{  
window.location=“p12.php”;
} 
setTimeout('Redirect()',5000);
p12.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>

函数重定向()
{  
window.location=“p2.php”;
} 
重定向();
p2.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秒。如果是,请让他们查看页面。然后删除数据库中的值。

如果我理解你的问题,你可以使用会话或COOKIES…提醒一下:标题可能被欺骗,这是非常弱的“安全性”。如果需要修补,或者需要额外加密,请告诉我security@Birdy顺便说一句,在测试时,请记住,一旦设置了会话,您必须在浏览器中删除会话,或使用incognito再次测试。如果您想在用户查看一次页面后自动取消授权,您还可以在页面底部添加
unset($\u SESSION['secret\u key'])
。@userbd非常棒!但也许用户不需要在每次到达网站时都重新授权page@Skarlinski,干得好。。它工作正常。感谢您花时间提供帮助,我相信其他处于相同位置的人将来也会依赖您的代码。我做了一个小的简短的编辑,只是为了提供我所做的轻微修改,这非常适合我的特殊需要,只是为了把它发布给未来的观众。希望你得到你应得的信用,因为我对你的信用限制为+1!再次感谢。我不知道为什么p12.php不能正确重定向。它对我有用。你有托管版本吗?我已经更新了p12的代码,添加了一个JavaScript重定向,作为php重定向失败时的故障保护。我猜测p2会改变的原因是缓存(因为没有动态内容,所以没有任何东西会改变的原因)。若要限制对该页面的访问,您需要将“echo”welcome.you can view this page.;“替换为您的页面内容。我还没有机会重试上述代码。我稍微修改了斯卡林斯基的答案,效果非常好,因此我接受了他的答案。很抱歉,我不能接受这两个答案,因为你们两个都花了自己的时间去帮助和帮助,这是值得表扬的。我希望人们在将来看到这一点,给你们两个应得的+1。再次感谢您,如果明天有任何问题,我会告诉您,因为现在是凌晨2点,我要睡觉了!谢谢