Login/Logout Time

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
HSKrustofsky
Forum Newbie
Posts: 14
Joined: Sun Oct 26, 2008 11:45 pm

Login/Logout Time

Post by HSKrustofsky »

I am attempting to record the login/logout time of a user. I am saving the times in the database(varchar), and I am successful with saving the login time, but the logout time is what I am having an issue with.

Login Code:

Code: Select all

...
	//Create query
	$qry="SELECT * FROM members WHERE login='$login' AND passwd='".md5($_POST['password'])."'";
	$result=mysql_query($qry);
	
	//Check whether the query was successful or not
	if($result) {
		if(mysql_num_rows($result) == 1) {
			//Login Successful
			session_regenerate_id();
			$member = mysql_fetch_assoc($result);
			$_SESSION['SESS_MEMBER_ID'] = $member['member_id'];
			$_SESSION['SESS_FIRST_NAME'] = $member['firstname'];
			$_SESSION['SESS_LAST_NAME'] = $member['lastname'];
			$_SESSION['SESS_LOGIN'] = $member['login'];
			$login = $_SESSION['SESS_LOGIN'];
			$time = date("h:i:s");
			
			mysql_query("INSERT INTO session (login, li_time) VALUES ('$login', '$time')");
			
			session_write_close();
			header("location: member-index.php");
			exit();
		}else {
			//Login failed
			header("location: login-failed.php");
			exit();
		}
	}else {
		die("Query failed");
	}
...
Logout Code

Code: Select all

...
		$qry="SELECT * FROM session";
		$result=mysql_query($qry);
		
		$session = mysql_fetch_assoc($result);
		$_SESSION['SESS_ID'] = $session['session_id'];
		$logout = $_SESSION['SESS_ID'];
		$time = date("h:i:s");
	
		mysql_query("UPDATE session SET lo_time = '$time' WHERE session_id = '$logout'");
	
	//Unset the variables stored in session
		unset($_SESSION['SESS_MEMBER_ID']);
		unset($_SESSION['SESS_FIRST_NAME']);
		unset($_SESSION['SESS_LAST_NAME']);
...
My guess is that it kills the session before I am able to record a logout time, or maybe I am completely wrong. Any suggestions?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Login/Logout Time

Post by Jonah Bron »

You can't really know when the person logs out for sure. To put in a logout time, you'd have to require them to click the "logout" button, and that's not consistent. The best you can do is to set the logout time every time the user loads a page. The database will ultimately hold the time of the last page load of the user.
HSKrustofsky
Forum Newbie
Posts: 14
Joined: Sun Oct 26, 2008 11:45 pm

Re: Login/Logout Time

Post by HSKrustofsky »

The point is to record the time when the person click on the logout button/link. I've got it to where it where enter a login time, then when I click to logout it stays NULL, but if I login, it will create a new row(as I need it to do), and when I logout the new record is NULL, but the first record updates the logout time. I tested again, creates a new row, enters the login time, and when I logout, again the first record updates with a new time. It seems that the session is truly never killed, or when I start the session, it believe the session_id is always 1.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Login/Logout Time

Post by Jonah Bron »

I see. Honestly, if I were you, I'd use a combination of the two methods. Otherwise you'll have a lot of sessions that don't have a logout time at all.

But to address your problem more directly, the issue appears to be in your SQL query. In the login page, you set the "login" column (whatever that is), but no "session_id" column. Then you try to access the session row by the "session_id" column later in logout, but it's not defined. And why are you querying for all rows in the logout page?
HSKrustofsky
Forum Newbie
Posts: 14
Joined: Sun Oct 26, 2008 11:45 pm

Re: Login/Logout Time

Post by HSKrustofsky »

I reference the login column because I am to enter the persons username in the login column(I should probably change the name from login to username), and I have the session_id to auto-increment when created, and I am hoping that the session would recognize the session_id.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Login/Logout Time

Post by Jonah Bron »

Basically what the logout code does in set the logout time to the first item it grabs from the database. What you want to do is store session_id() in the session_id column in your table.

http://php.net/session-id
Post Reply