Jump to content

Last Active At...? How is this done?


unsider

Recommended Posts

I'm assuming you would create a timestamp for the user when he registers? And the timestamp would track when the session was last active/ ended, for that particular user. Is converting a time stamp into a last active on: jan 4, 2006 as simple as a echoing the timestamp in a date format?

 

If I'm not making sense or any of what I said is incorrect, please correct me, I'm here to learn. I've written a little piece of code, can someone explain the 'last active' concept to me through this code (if possible)?

 

Also keep in mind this was written quickly, I'm tired, and probably gonna check this in the morning. So feel free to correct ANY mistakes I might have made.

 

Thanks.

 

<?php
function timestamp(){
   global $database;
   $q = "SELECT timestamp"
       ."FROM "user" ORDER BY timestamp DESC, timestamp";
   $result = $database->query($q);
   $num_rows = mysql_numrows($result);
   if(!$result || ($num_rows < 0)){
      echo "Error displaying info";
      return;
   }
   if($num_rows == 0){
      echo "Database table empty";
      return;
   }    
     $time   = mysql_result($result,"timestamp"); 
     echo "$time";
}
?>

 

 

 

Link to comment
Share on other sites

$q = "SELECT timestamp"

      ."FROM "user" ORDER BY timestamp DESC, timestamp";

 

Is entirely valid.

 

 

 

 

The problem is that you're not updating the timestamp.  Everytime the user requests a page, you know they're active, yes?  That would mean that you should update the timestamp then.

Link to comment
Share on other sites

Well I am updating the timestamp (in my real project :)), if the user is logged in that is, session values set, etc.., and requests a page, I guess now I'm just asking how I would output the last recorded timestamp of that particular user in a....

 

Last Active On: Feb 11, 2030

 

fashion.

 

And thanks for clarifying that, I wasn't absolutely sure if that SQL was valid.

Link to comment
Share on other sites

i have a field in my database that has a timestamp in it the time stamp is updated everytime the user presses login or access a page and it detects an auto login cookie and no session setup.

if you want it to just record every time a user views a page add a line at the top of your code after you authenticate the user

<?php
mysql_query("UPDATE `users` (`last_active`) VALUES ('".time()."');");
?>

 

Scott.

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.