Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
Thread: user last login date
05-09-2013, 09:14 PM #1
- Join Date
- Jan 2011
- The Pleiades
- Thanked 28 Times in 28 Posts
user last login date
I want to be able to display the date at which a user last logged into the site. I have created a table to store the details of course and i've decided to add a field at the end called last_login.
My intentions are after verifying the details are correct, to then update their last_login field with a timestamp, then display the profile in my usual way. My first thoughts are that this would require another query which I don't want to do. I have posted here to see if anyone can advise me on my code and see what the best way would be to update that field.
Does it require another query?
Here is my code:
if($errors == "")
//check what the values were
if($nrow == 1)
//successful, set session
$_SESSION['user_name'] = $username;
$errors .= "You did not enter the correct username and/or password.<br />";
$conn = new mysqli("localhost","root","","****") or die("Error creating connection.");
$stmt = $conn->prepare("SELECT username FROM users WHERE username=? && password=?") or die("Error: ".mysqli_error($conn));
$nrow = $stmt->num_rows;
$stmt = $stmt->fetch();
05-09-2013, 09:46 PM #2
- Join Date
- Jun 2009
- Thanked 19 Times in 19 Posts
Yes it would require another query, we do get fixated on keeping them to a minimum, however 2 queries wont break the bank! You have three things you are trying to accomplish
1) Login client (checking credentials)
2) Display clients last login time
3) Update clients last login time
If you did it the way you are you would in fact need 3 queries. However if you also get the last login time from the column that stores it (join might be necessary, dont know your table layouts) during login you reduce that down to 2 querys.
1) Login client (checking credentials) & get last login time
2) Update login
05-09-2013, 09:52 PM #3
- Join Date
- Sep 2002
- Saskatoon, Saskatchewan
- Thanked 2,659 Times in 2,628 Posts
If you intend to perform an update, your only option is to issue an update query.
Since you are using sessions, your only option is to update with either the current time, or the maximum anticipated time they will be around, which is the current time plus your timeout. If they log out explicitly, you can also set it then. The only alternative is to disable garbage collection, move the sessions to your own local directories, then iterate and parse every file older than your timeout and extract information out of them to update with prior to unlinking them. Note if you do this, despite how similar it looks to it, sessions are *not* encoded with serialize. You will need to write a parser for it, or invoke the session_decode whilst juggling faux sessions. And no that isn't fun.
If you were using database constructed and controlled, it would be a bit of a different story. Still need to issue the updates though.
header('HTTP/1.1 420 Enhance Your Calm');