techiefreak05 Posted July 31, 2006 Share Posted July 31, 2006 How do I show the last login of a user, like when a user logins into my site, it says : "Last Login: DATE" Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/ Share on other sites More sharing options...
pedrobcabral Posted July 31, 2006 Share Posted July 31, 2006 When he does login save this in a text or database - date("y-m-d g:i:s"); Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/#findComment-66318 Share on other sites More sharing options...
tomfmason Posted July 31, 2006 Share Posted July 31, 2006 Now you can use this as well[code=php:0]// this is assumming that you have a field called lastlogin_date in your database$sql = "SELECT DATE_FORMAT(lastlogin_date, '%M %D %Y') as formated_date From users WHERE username ='$username'";$check_login_date = mysql_query($sql);if (!$check_login_date) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit;} while ($rw = mysql_fetch_assoc($check_login_date)) { echo 'Last Login Date: <b>' . $rw['formated_date'] . '</b>';}mysql_free_result($check_login_date);[/code]This would return the date like this [b]June 31 2006[/b]Hope this helps,Tom Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/#findComment-66325 Share on other sites More sharing options...
tomfmason Posted July 31, 2006 Share Posted July 31, 2006 You know here is a better way of doing this. Place this in your login script after a sucessful login[code=php:0] // this is assumming that you have a field called lastlogin_date in your database$sql = "SELECT DATE_FORMAT(lastlogin_date, '%M %D %Y') as formated_date From users WHERE username ='$username'";$check_login_date = mysql_query($sql);if (!$check_login_date) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit;} while ($rw = mysql_fetch_assoc($check_login_date)) { $_SESSION['lastlogin'] = $rw['formated_date'];}mysql_free_result($check_login_date);// now we will update the date with the date today.$q = "UPDATE users SET lastlogin_date ='now()' WHERE username ='$username' ";$change_date = mysql_query($q) or die(mysql_error());[/code] Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/#findComment-66328 Share on other sites More sharing options...
tomfmason Posted July 31, 2006 Share Posted July 31, 2006 I almost forgot now you can display this date on any page like this.[code=php:0]<?phpecho 'Last login: <b>' . $_SESSION['lastlogin'] . '</b>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/#findComment-66330 Share on other sites More sharing options...
pedrobcabral Posted July 31, 2006 Share Posted July 31, 2006 [quote author=tomfmason link=topic=102433.msg406440#msg406440 date=1154352352]I almost forgot now you can display this date on any page like this.[code=php:0]<?phpecho 'Last login: <b>' . $_SESSION['lastlogin'] . '</b>';?>[/code][/quote]Better not forget to session_start(); Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/#findComment-66344 Share on other sites More sharing options...
tomfmason Posted July 31, 2006 Share Posted July 31, 2006 if you should want to add a time to the last login do this. This is assuming you have a field named lastlogin_date and another named lastlogin_time[code=php:0]$sql = "SELECT DATE_FORMAT(lastlogin_date, '%M %D %Y') as formated_date, TIME_FORMAT(lastlogin_time, '%r') as formated_time From users WHERE username ='$username'";$check_login_date = mysql_query($sql);if (!$check_login_date) { echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit;} while ($rw = mysql_fetch_assoc($check_login_date)) { $_SESSION['lastlogin_date'] = $rw['formated_date']; $_SESSION['lastlogin_time'] = $rw['formated_time'];}mysql_free_result($check_login_date);// now we will update the date with the date today.$q = "UPDATE users SET lastlogin_date ='now()', lastlogin_time ='now()' WHERE username ='$username' ";$change_date = mysql_query($q) or die(mysql_error());[/code]Now you can display this in your pages[code=php:0]echo 'Last login: <b>' . $_SESSION['lastlogin_date'] . ' at ' . $_SESSION['lastlogin_time']</b>';[/code]This will return Last Login: June 31 2006 at 09:00 AmThe time and date format will be the local format for the server that you are using. If you are wanting to change the date/time format to your local then see the mysql manual: [url=http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#id2855815]Date/Time functions[/url]Good luck,Tom Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/#findComment-66353 Share on other sites More sharing options...
tomfmason Posted July 31, 2006 Share Posted July 31, 2006 [quote author=pedrobcabral link=topic=102433.msg406454#msg406454 date=1154353601]Better not forget to session_start();[/quote]Yea you are right.lol. How could I forget that. I guess that I assumed that he would already know that. But you know what they say about assumptions Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/#findComment-66355 Share on other sites More sharing options...
Ifa Posted July 31, 2006 Share Posted July 31, 2006 Ofcource, <?php ?> would be nice too ;D Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/#findComment-66358 Share on other sites More sharing options...
tomfmason Posted July 31, 2006 Share Posted July 31, 2006 I meant for this code snippett to be used in the login script, after a sucessful login. He is wanting to post the last login date. So he will need to read the last date and then update it after it is put into session variables. Link to comment https://forums.phpfreaks.com/topic/16097-last-login-how/#findComment-66364 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.