Login/Logout Time
Posted: Sun Jan 30, 2011 7:24 pm
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:
Logout Code
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?
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");
}
...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']);
...