Jump to content

Archived

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

techiefreak05

Last Login - HOW??

Recommended Posts

How do I show the last login of a user, like when a user logins into my site, it says : "Last Login: DATE"

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
I almost forgot now you can display this date on any page like this.
[code=php:0]
<?php
echo 'Last login: <b>' . $_SESSION['lastlogin'] . '</b>';
?>
[/code]

Share this post


Link to post
Share on other sites
[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]
<?php
echo 'Last login: <b>' . $_SESSION['lastlogin'] . '</b>';
?>
[/code]
[/quote]

Better not forget to session_start();

Share this post


Link to post
Share on other sites
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 Am

The 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


Share this post


Link to post
Share on other sites
[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

Share this post


Link to post
Share on other sites
Ofcource, <?php ?> would be nice too  ;D

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

×

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.