【问题标题】:How to stop going to next page after session expired?会话过期后如何停止进入下一页?
【发布时间】:2018-12-05 23:09:19
【问题描述】:

在测验任务中,我已经完成了刷新页面时为每个页面设置的时间 它会在 1 分钟后过期。但是如果我点击下一步,1分钟后没有刷新页面,它会进入下一个测验页面。我可以阻止它进入下一页。 这是我的代码

    <html>
<head>
<style>
#user
{



color:blue;
font-size:16px;
}
#pass
{

color:blue;
font-size:16px;
}

</style>
</head>
<body>
<table>
<form action="quizpage2.php" method="post">
<tr>
<td><div id="user">Username:</div></td></br>
<td><input type="text" name="user"></td>
</tr>

<tr>
<td><div id="pass">Password:</div></td>
<td><input type="text" name="pass"></td>
</tr>

<tr>
<td><input  type="submit" value="submit"></td>
</tr>

</form>
</table>
</body>
</html>

page2

session_start();

$AB=$_POST["user"];

$CD=$_POST["pass"];


$_SESSION["new"]=$AB;


$EF=array("paul", "andrew", "steven" ,"don");

$GH=array(123,    456,    789,      000);

if(($AB==$EF[0])&&($CD==$GH[0]))
{

$_SESSION["new"]=$EF[0];
$_SESSION["start"]=time();
$_SESSION["expire"]=$_SESSION["start"]+(1*60);

echo "<script> alert('login successfull')</script>";
echo "<script>window.location.assign('quizpage3.php')</script>";
}
?>





page3-

<?php



session_start();



if(!isset($_SESSION["new"]))
{
echo"<p align='center';>Please login Again";
echo"<a href='quizpage1.php'>Click here</a></p>";
//header("location:quizpage1.php");
}

else
{
$now=time();

if($now>$_SESSION['expire'])
{
session_destroy();
echo"<p align='centre;'>your session is expired!<a href='quizpage1.php'>Login Here</a></p>";
echo"<span style='float:right;'><a href='logoutpage.php'>Logout</a></span>";
}


else
{

echo"<html>";
echo"<head>";
echo"<script src='https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js'></script>";
echo"<script>

 $(document).ready(function() {
window.history.forward(1);
});

</script>";


echo"<style>
    #sun
    {
        background-color:lightblue;
        width:60%;
    }

</style>";


echo"</head>";
echo"<body>";

echo"<form action='quizpage4.php' method='post'>";

echo"<div id='sun'>";
echo"<tr>";
echo"<td>Which of the following part of the Sun is visible by human?</td></br>";
echo"<td><input type='radio' name='sun' value='Photosphere'>Photosphere</td>.</br>";
echo"<td><input type='radio' name='sun' value='Corona'>Corona</td>.</br>";
echo"<td><input type='radio' name='sun' value='Chromosphere'>Chromosphere</td>.</br>";
echo"<td><input type='radio' name='sun' value='Core'>Core</td>.</br>";
 echo"</tr>";

echo"<tr>";

echo"<td><input type='submit' value='Nextpage'></td>";

echo"</tr>";
echo"</div>";
echo"</form>";

echo"</table>";
echo"</body>";
echo"</html>";

}

}


?>




    enter code here

Logoutpage-

<?php
session_start();
session_destroy();
header("location:quizpage1.php");
?>de here

我有 7 个测验页面,一旦每个测验页面在一分钟后刷新,它就会返回登录页面,但如果一分钟后单击下一步按钮则不刷新,它将进入下一个测验页面。我想在 1 分钟后停止进入下一个测验页面。 提前致谢

【问题讨论】:

    标签: php session redirect


    【解决方案1】:

    你已经使用session time expirere-login

    如果页面刷新整个呼叫重新加载并检查您的时间到期

    $now=time();
    
    if($now>$_SESSION['expire'])
    {
    session_destroy();
    echo"<p align='centre;'>your session is expired!<a href='quizpage1.php'>Login Here</a></p>";
    echo"<span style='float:right;'><a href='logoutpage.php'>Logout</a></span>";
    }
    

    但是当您在一分钟后单击next button 时,每次都访问下一页,因为 您的表单已提交,其操作调用并在每次会话时间到期后转到下一页

    echo"<form action='quizpage4.php' method='post'>";
    

    这里的错误是您的表单操作调用按钮单击和下一页访问而不检查session

    所以你需要删除form action like action="" 并添加next button like 的名称

    echo"<td><input type='submit' value='Nextpage' name='submit'></td>";
    

    检查按钮是单击并且会话不会比下一页调用过期

    if(isset($_POST['submit']) && !empty($_POST['submit']))
    {
        echo "<script>window.location.assign('quizpage4.php')</script>";
    }
    

    quizpage2.php 开始检查time expiry 并重新加载登录页面

    $now=time();
    
    if($now>$_SESSION['expire'])
    {
        //echo 1;exit;
        session_destroy();
        echo"<p align='centre;'>your session is expired!<a href='quizpage1.php'>Login Here</a></p>";
        echo"<span style='float:right;'><a href='logoutpage.php'>Logout</a></span>";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-11
      • 2016-12-20
      • 1970-01-01
      • 2013-11-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多