In quiz task, I have done the time set for each page when I refresh the page it expires after 1 minute. But after 1 minute without refreshing the page if I click next, it is going to the next quiz page. I can stop this from going to next page. here is my code
<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
I have 7 quiz page once each quiz page refreshed after one minute it is going back to the login page but without refreshing if click next button after one minute it is going to next quiz page. I want to stop this going to next quiz page after 1 minute. Thanks in advance
You have use session time expire
to re-login
if page refresh whole call reload and check your time expiry
$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>";
}
but when you click next button
after one minute its every time access next page because your form was submit and its action call and goes to next page every time after session time was expiry
echo"<form action='quizpage4.php' method='post'>";
mistake is here your form action called on button click and next page access without checking session
so you need to remove form action
like action=""
and add name of next button
like
echo"<td><input type='submit' value='Nextpage' name='submit'></td>";
and check button is click and session not expire than next page call
if(isset($_POST['submit']) && !empty($_POST['submit']))
{
echo "<script>window.location.assign('quizpage4.php')</script>";
}
OR
check time expiry
on quizpage2.php
starting and reload login page
$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>";
}