Jump to content

PHP session timer


strategos

Recommended Posts

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 :D

Link to comment
https://forums.phpfreaks.com/topic/261666-php-session-timer/
Share on other sites

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'); 
?> 

Link to comment
https://forums.phpfreaks.com/topic/261666-php-session-timer/#findComment-1340940
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.