Jump to content

[SOLVED] REcord last login date


samona

Recommended Posts

Hi all,

 

I was hoping someone could help me with recording a user's last login date.  I have a form that users use to login.  There is an sql statement that checks whether the person is a valid user and allows them to login if a record matches.  Is it at this time that I have to make another query to insert the date of the login?

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/105751-solved-record-last-login-date/
Share on other sites

When you do your query, just write NOW(), no need to use a variable or a PHP Date function.

 

Yeah... Should have thought of that, but if you don't like the format of NOW() you can change it, but there are many ways to do this:

 

NOW

mysql_query("UPDATE table SET lastLog = 'NOW()' WHERE id = '{$row['userID']}");

 

date

$date = date("m/d/Y g:i:s");
mysql_query("UPDATE table SET lastLog = '$date' WHERE id = '{$row['userID']}");

 

UNIX TIME STAMP

$date = strtotime("now");
mysql_query("UPDATE table SET lastLog = '$date' WHERE id = '{$row['userID']}");
/* 
Most versatile, use the date function to format it easily
This also makes it easy to sort/math/compare the dates if needed
*/

The following is my code.  Do I save it in a session variable like I have below to be able to display it on the other page?

 

 

if ($user_name && $user_password)
{
	$query = "Select User_ID, First_Name, User_Name, Last_Name, Is_Admin, Last_Login FROM Users WHERE User_Name='$user_name' AND User_Password='$user_password'";
	$result = @mysql_query($query);
	$row = mysql_fetch_array($result, MYSQL_NUM);


if($row)
	{
		$_Session['last_login'] = $row['Last_Login'];
		mysql_query("UPDATE table SET Last_Login = 'NOW()' WHERE id = '{$row['user_ID']}");
		$result = @mysql_query($query);

		//Start the session, register the values and redirect.
		session_name('YourVisitID');
		session_set_cookie_params(900, '/','website.com');
		session_start();

		$_SESSION['first_name'] = $row[1];
		$_SESSION['user_id'] = $row[0];
		$_SESSION['user_name'] = $row[2];
		$_SESSION['last_name'] = $row[3];
		$_SESSION['is_admin'] = $row[4];
		header("Location: ./loggedin.php");

		exit();
	}

Archived

This topic is now archived and is closed to further replies.

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