Jump to content

Last Login - HOW??


techiefreak05

Recommended Posts

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
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]
Link to comment
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


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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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