strategos Posted April 26, 2012 Share Posted April 26, 2012 I don't know if this can be done in PHP, but what I'm wanting to do is calculate the amount of time a user has spent in a session. I am planning on making a script that allows users to race through questions and the person who does it the fastest is the winner. Could someone help me out with this? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/261666-php-session-timer/ Share on other sites More sharing options...
Jessica Posted April 26, 2012 Share Posted April 26, 2012 When the session starts, store the time. When you're ready to finish, store that time. Subtract. Quote Link to comment https://forums.phpfreaks.com/topic/261666-php-session-timer/#findComment-1340852 Share on other sites More sharing options...
xyph Posted April 26, 2012 Share Posted April 26, 2012 You probably want to store the time at each question as well, to prevent users from simply skipping to the last question. Quote Link to comment https://forums.phpfreaks.com/topic/261666-php-session-timer/#findComment-1340857 Share on other sites More sharing options...
reelmark Posted April 27, 2012 Share Posted April 27, 2012 This code will expire session in 30 minutes. look into it you will find solution from the code if you want to change the time, just change the 30 with you desired time and do not change * 60 : this will gives the minutes Login.php <?php session_start(); ?> <html> <form name="form1" method="post"> <table> <tr><td>Username </td><td><input type="text" name="text1"></td></tr> <tr><td>Password</td><td><input type="password" name="pwd"></td></tr> <tr><td><input type="submit" value="SignIn" name="submit1"> </td></tr> </table> </form> </html> <?php if($_POST['submit1']) { $v1 = "FirstUser"; $v2 = "MyPassword"; $v3 = $_POST['text']; $v4 = $_POST['pwd']; if($v1 == $v3 && $v2 == $v4) { $_SESSION['luser'] = $v1; $_SESSION['start'] = time(); // taking now logged in time $_SESSION['expire'] = $_SESSION['start'] + (30 * 60) ; // ending a session in 30 minutes from the starting time header('Location: http://localhost/somefolder/homepage.php'); } else { echo "Please enter Username or Passwod again !"; } } ?> HomePage.php if(!isset($_SESSION['luser'])) { echo "Please Login again"; echo "<a href='http://localhost/somefolder/login.php'>Click Here to Login</a>"; } else { $now = time(); // checking the time now when home page starts if($now > $_SESSION['expire']) { session_destroy(); echo "Your session has expire ! <a href='http://localhost/somefolder/login.php'>Login Here</a>"; } else { //starting this else one [else1] ?> <!-- From here all HTML Coding can be done --> <html> Welcome <?php echo $_SESSION['luser']; echo "<a href='http://localhost/somefolder/logout.php'>LogOut</a>"; ?> </html> <?php } } ?> LogOut.php <?php session_start(); session_destroy(); header('Location: http://localhost/somefolder/login.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/261666-php-session-timer/#findComment-1340940 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.