Jump to content


Photo

Last Login - HOW??


  • Please log in to reply
9 replies to this topic

#1 techiefreak05

techiefreak05
  • Members
  • PipPipPip
  • Advanced Member
  • 494 posts
  • LocationER, MN

Posted 31 July 2006 - 12:20 PM

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

http://tyny.us/

#2 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 31 July 2006 - 12:37 PM

When he does login save this in a text or database - date("y-m-d g:i:s");

#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 31 July 2006 - 01:09 PM

Now you can use this as well
// 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);


This would return the date like this June 31 2006

Hope this helps,
Tom


Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 31 July 2006 - 01:18 PM

You know here is a better way of doing this. Place this in your login script after a sucessful login

// 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());

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#5 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 31 July 2006 - 01:25 PM

I almost forgot now you can display this date on any page like this.
<?php
echo 'Last login: <b>' . $_SESSION['lastlogin'] . '</b>';
?>

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#6 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 31 July 2006 - 01:46 PM

I almost forgot now you can display this date on any page like this.

<?php
echo 'Last login: <b>' . $_SESSION['lastlogin'] . '</b>';
?>


Better not forget to session_start();

#7 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 31 July 2006 - 02:02 PM

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

$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());


Now you can display this in your pages

echo 'Last login: <b>' . $_SESSION['lastlogin_date'] . ' at ' . $_SESSION['lastlogin_time']</b>';

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: Date/Time functions

Good luck,
Tom



Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#8 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 31 July 2006 - 02:04 PM


Better not forget to session_start();


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

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#9 Ifa

Ifa
  • Members
  • PipPipPip
  • Advanced Member
  • 88 posts
  • LocationRauma, Finland

Posted 31 July 2006 - 02:08 PM

Ofcource, <?php ?> would be nice too  ;D
I'm just a guy who likes to code for fun...

#10 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 31 July 2006 - 02:21 PM

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.

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users