/    Sign up×
Community /Pin to ProfileBookmark

PHP Last Logged In Time

Hello,

So I am creating this login admin site and I would like to keep of track of when users last logged in and then show them that message in the welcome screen. Kind of how webdeveloper.com does it for our accounts. I am a little confused on where to put the script. At first I just send the date and time to a table in my database when the user logs in but then I realized everytime they login it will be overwritten by the most current login, so that doesnt seem to work.

The other thing I was thinking was to just log the date and time when they logout but then if they dont officially logout the date and time will not be updated.

How do you guys usually solve this problem?

Thanks,
Seth

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogNov 16.2005 — When validating the user login, grab the login time value and save it in a session variable, [i]then[/i] update the field with the current time. Use the value in the session variable to display the "last login time" on each page. (I'm assuming you're using PHP sessions for login control?)
Copy linkTweet thisAlerts:
@mididelightauthorNov 16.2005 — a little confused, how does the session variable know the last time they logged in? Also, when do I get the date and time of there last login?
Copy linkTweet thisAlerts:
@SheldonNov 16.2005 — Post your login script and db table names and well show you.

[url=http://php.inspire.net.nz/manual-lookup.php?pattern=sessions&lang=en]Sessions[/url]
Copy linkTweet thisAlerts:
@NogDogNov 16.2005 — Presumably as part of your login validation you query the database for a match on the login name and password. When doing that query, also select the (new) login_time field, something like (depending on your table/column names and password implementation):
[code=php]
$query = "SELECT name, password, time FROM users WHERE " .
"name='{$_POST['name']}' AND password = PASSWORD('$_POST['password']')";
$result = mysql_query($query) or die(mysql_error());
if($result and mysql_num_rows($result) == 1)
{
session_start();
$row = mysql_fetch_assoc($result);
$_SESSION['last_login'] = $row['time']
$update = "UPDATE users SET time = NOW() WHERE name = '{$_POST['name']}'";
$result = mysql_query($update) or die(mysql_error());
# continue with normal processing for valid login
}
else
{
# whatever you do for invalid logins
}
[/code]
Copy linkTweet thisAlerts:
@mididelightauthorNov 17.2005 — oh i get it, get the lastlogin, put it in a sessino and then overright that with the one. use the lastlogin session to display when they last logged in. brilliant! thanks!
Copy linkTweet thisAlerts:
@beginnerzNov 17.2005 — wat about i need to insert all the record who have logged in , date and time for each record, not only just updating?
Copy linkTweet thisAlerts:
@mididelightauthorNov 17.2005 — here is the script i used:

=================================================

$HTTP_SESSION_VARS['admin'] = true;

$_SESSION['name'] = $_POST['name'];

// LOG USER ACCESS

$user = $_SESSION["name"];

$user = strtolower($user);

$user = ucwords($user);

$user = trim($user);

//Date and Time

$date = date('m/d/y - h:i A');

include('includes/dbconnect.php');

$db = mysql_connect($database_host, $database_username, $database_password) or die ("<b>Error:</b> Could not connect to the database.");

mysql_select_db($database_name, $db) or die ("<b>Error:</b> The database could not be selected.");

// Get Last Login

$getll = mysql_query("SELECT lastlogin FROM users WHERE name = '$user' ") or die( "<b>Error:</b> Something went wrong, could not edit link status.");

list($lastlogin) = mysql_fetch_row($getll);

// Set session variable

$_SESSION['lastlogin'] = $lastlogin;

// Update New LastLogin

$updatelog = mysql_query("UPDATE users SET lastlogin = '$date' WHERE name = '$user' ") or die( "<b>Error:</b> Something went wrong, could not edit link status.");

mysql_close($db);

header("Location: /admin/panel.php");
==========================================



So I basically just get the last login, set it to a session variable then overwrite that entry with the new login date and time.

works like a charm!
×

Success!

Help @mididelight spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...