Jump to content

[SOLVED] Custom PHP session timeouts


JoelRocks

Recommended Posts

Hey guys, i have been using the tutorial on here to get this far, but i cannot work out what to do next, i wanted to create a system that allows the user to login for 30 minutes and then their session ends, they have to login again...

 

This is the code that i have used so far, all working thanks to other guys on the forums:

 

?php 
$hostname= "localhost";
$user= "remotepa_framewo";
$password = "";
$username = trim($_POST["ipt_Username"]);
$user_password = sha1($_POST["ipt_Password"]);

	if ($_POST["ipt_Username"] == "")
	{
	?>
		<p>
			Please enter your user details.
		</p>
		<form action="login.php"  method="POST" >
		Username
		<br />
		<input type="text" name="ipt_Username">
		<br />
		Password 
		<br />
		<input type="text" name="ipt_Password">
		<br />
		<input type="submit"  value="Login">
		</form>
	<?
	}
	else 
	{
		$conn = @mysql_connect(  $hostname, $user, $password )
		or die ("Could not connect to server");
		$db = @mysql_select_db("remotepa_framework", $conn)
		or die ("Could not connect to database");
		$sql = "SELECT * FROM users WHERE username=\"$username\" and password = \"$user_password\"";
		$result = @mysql_query( $sql, $conn)
		or die ("Could not execute query");
		$num = mysql_numrows($result);
		if ($num != 0)
		{

       			session_start(); 
     			session_register('username'); 
        		session_register('password'); 
        		$sess_id = session_id(); 
        		header("Location: index.php?PHPSESSID=$sess_id&new_login=1"); 
			echo ("User Exists");
		}
		else 
		{
			echo ("User does not exist");
		}
	}
	?>

 

Thanks

 

Joel

Link to comment
Share on other sites

The problem with cookies is that users can turn off cookies and then bypass this system.

 

What you could do is set a session variable when they initially visit/login to the time they logged in. Then each time they try to access a page you subtract the current time from the time they logged in if this exceeds 30 minutes you clear their session, logged them out and send them back to start over :)

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.