会话过期后如何停止转到下一页?

时间:2018-06-27 06:49:41

标签: php session redirect

在测验任务中,刷新页面时我已经完成了为每个页面设置的时间  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分钟后停止转到下一个测验页面。 预先感谢

1 个答案:

答案 0 :(得分:0)

您已经使用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之类的action=""并添加next button之类的名称

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

OR

time expiry上检查quizpage2.php,然后重新加载登录页面

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