cyrixware Posted February 26, 2008 Share Posted February 26, 2008 A little problem regarding user time limit... I want to create a time limit every time the user access the page using user log-in. Just like the phpfreaks that the user wer going to specify how long does he/she login. Let say i choose 60 minutes so after 60 minutes the page will expire. So he/she will force to relog-in again. How to do that? thanks in advance. Quote Link to comment Share on other sites More sharing options...
cyrixware Posted February 26, 2008 Author Share Posted February 26, 2008 Coz i created a simple online quiz... Every person takes the exam will have atleast 60 minutes maximum time. After an hour the page will expire. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 Well, I assume to keep the person logged in from page to page you are storing some value(s) in the Session. If so, just store another value (call it logged in) that contains the unix timestamp from when they logged in. Then, each time you run your code to confirm they are logged in, check to see if it's been 60 minutes from that time. If so, destroy the Session, and send them to the login page. Make sense? Quote Link to comment Share on other sites More sharing options...
cyrixware Posted February 26, 2008 Author Share Posted February 26, 2008 I see, How to check to see if it's been 60 minutes from that time? Login Page: <?php session_start();?> <?php if($_REQUEST['Submit'] == "Login") { $username = $_REQUEST['username']; $passwrd = $_REQUEST['passwrd']; include("../connDB.php"); $sql = "SELECT * FROM table WHERE Username = '$username' AND Password = '$passwrd'"; if(!$q = mysql_query($sql)) { die(mysql_error()); } elseif(mysql_num_rows($q) == 0) { echo "<font size=2 face=Verdana color=red><b>Invalid Username or Password!</b></font><br>"; echo "<font size=2 face=verdana color=red><b>Try Again</b></font>"; } else { $r = mysql_fetch_assoc($q); $TID = '1'; $session_id = md5(date("Y-m-d H:i:s") . $TID); $sql1 = "UPDATE table SET SessionID= '$session_id' WHERE Type = '$TID'"; if(!$q1 = mysql_query($sql1)) { die(mysql_error()); } elseif(mysql_affected_rows() == 0) { echo "<font size=2 face=Verdana color=red>Failed to create session id!</font>"; } else { $_SESSION['session_id'] = $session_id; } } $session_id = $_SESSION['session_id']; if($session_id != null && $session_id != "") { ?> <script language=JavaScript> win = window.open('page.php?ID=<?=$session_id;?>','_self') </script> <? } } ?> Session: <?php session_start(); include("../connDB.php"); $session_id = $_SESSION['session_id']; $message = "Invalid or Expired session id..."; if($session_id == null || $session_id == "") { header("Location: login.php?Message=$message"); exit(); } $sql = "SELECT * FROM table WHERE SessionID = '$session_id'"; if(!$q = mysql_query($sql)) { header ("Location: login.php?Message=$message"); } if(mysql_num_rows($q) == 0) { $message = "Re-logon for security purposes..."; header ("Location: login.php?Message=$message"); } $r = mysql_fetch_assoc($q); ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 In the Login page, after this line: $_SESSION['session_id'] = $session_id; add this: $_SESSION['session_time'] = time(); It will store the current time as a unix timestamp then in the session file, after this: if($session_id == null || $session_id == "") { header("Location: login.php?Message=$message"); exit(); } add this: $session_time = $_SESSION['session_time']; if(!$session_time || time() - $session_time > 3600) { header("Location: login.php?Message=$message"); exit(); } It checks to make sure the difference between now and the time stored at the login is less then or equal to 60 minutes (3600 seconds) Quote Link to comment Share on other sites More sharing options...
cyrixware Posted February 26, 2008 Author Share Posted February 26, 2008 no need to create new field in my database? Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 I don't see the need Quote Link to comment Share on other sites More sharing options...
cyrixware Posted February 26, 2008 Author Share Posted February 26, 2008 In the Login page, after this line: $_SESSION['session_id'] = $session_id; add this: $_SESSION['session_time'] = time(); It will store the current time as a unix timestamp then in the session file, after this: if($session_id == null || $session_id == "") { header("Location: login.php?Message=$message"); exit(); } add this: $session_time = $_SESSION['session_time']; if(!$session_time || time() - $session_time > 3600) { header("Location: login.php?Message=$message"); exit(); } It checks to make sure the difference between now and the time stored at the login is less then or equal to 60 minutes (3600 seconds) Thanks a lot boss! Your the man! Quote Link to comment Share on other sites More sharing options...
cyrixware Posted February 26, 2008 Author Share Posted February 26, 2008 In the Login page, after this line: $_SESSION['session_id'] = $session_id; add this: $_SESSION['session_time'] = time(); It will store the current time as a unix timestamp then in the session file, after this: if($session_id == null || $session_id == "") { header("Location: login.php?Message=$message"); exit(); } add this: $session_time = $_SESSION['session_time']; if(!$session_time || time() - $session_time > 3600) { header("Location: login.php?Message=$message"); exit(); } It checks to make sure the difference between now and the time stored at the login is less then or equal to 60 minutes (3600 seconds) Thanks a lot boss! Your the man! Quote Link to comment Share on other sites More sharing options...
cyrixware Posted February 26, 2008 Author Share Posted February 26, 2008 By the way how to print the remaining time for this? <?php $time = time() - $session_time > 3600; echo "$time";//is this correct? ?> Quote Link to comment Share on other sites More sharing options...
cyrixware Posted February 26, 2008 Author Share Posted February 26, 2008 <?php $var = (time()- ($session_time > 36000)); //$var = 'test'; echo "$var"; ?> The output is a series of numbers ex. 1204004913 Is there any way how to echo the remaining time exactly? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 26, 2008 Share Posted February 26, 2008 php.net/date Quote Link to comment Share on other sites More sharing options...
DarkerAngel Posted February 26, 2008 Share Posted February 26, 2008 I think you have it backwards from what he wants, I think he wants to display the remaining time, rather than the time to expire. Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 26, 2008 Share Posted February 26, 2008 http://www.phpfreaks.com/forums/index.php/topic,172924.0.html Quote Link to comment Share on other sites More sharing options...
cyrixware Posted February 26, 2008 Author Share Posted February 26, 2008 hehehehe going complicated... Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 26, 2008 Share Posted February 26, 2008 A little background on that 'series of numbers'. Unix time is "the number of seconds elapsed since midnight Coordinated Universal Time(UTC) of January 1, 1970". You can read more about it here: http://en.wikipedia.org/wiki/Unix_time It's very convenient because it is a simple integer, language independent, and not dependent on any timezone information. The downside is that it's not human readable. Two ways to see what time a timestamp actually is, is to go to http://www.unixtimestamp.com/ and put it in that form, or use PHP the date() function: <?php echo date('r',$time); ?> Check out the PHP doc for more information on formating the date output by the date function. Now that we know a little bit about timestamps, let's get into time difference. Since a timestamp is the number of seconds from a particular point (1970), if we subtract two timestamps(timeStart from timeEnd), we will get the number of seconds between those two times. This again is not human readable. Enter the post referenced by jesirose. Read it over, specifically this post: http://www.phpfreaks.com/forums/index.php/topic,172924.msg767511.html#msg767511 Hope that helps clarify some stuff Quote Link to comment Share on other sites More sharing options...
cyrixware Posted February 26, 2008 Author Share Posted February 26, 2008 Cool. Thanks again rhodesa! Quote Link to comment 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.