Jump to content

Creating Time Limit


cyrixware

Recommended Posts

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. :)

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

?>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.