PHP Cookies: Get Users Last Login Time

I have two questions really:

  1. How do I create a cookie that expires in an hour?

setcookie("username", $username, time()+604800); /* Expires in a week */

  1. How do I create a cookie that figures out a users last login time? (This one I have not a clue.)

Thanks for the help! I seem to be having at least one question a day… must be annoying, so thank you! :faq1:

Just create a cookie that stores the current time() for the value. For example:

setCookie('lasttime', time(), time()+604800);

Then just call $_COOKE[‘lasttime’] when you want their last login time.

As for a cookie that expires in 1 hour:

setCookie('blah', 'blah', time()+(60*60));

60 seconds times 60 minutes = 1 hour. (Or you can use 3600 instead of “+(60*60)”

Awesome thirteen, exactly what I was looking for. Thanks!

I’m running into a problem. I have the following code in the header of all of my pages. The portion of code that is supposed to display how many new “Kokonuts” exist (based on users last login) doesn’t seem to work.

Am I setting this up correctly?


$username = addslashes($_POST['username']);
$salt = "stophacking";
$password = md5($_POST['password']);

if (isset($_COOKIE["username"])){

   $lasttime = $HTTP_COOKIE_VARS["lasttime"];
   $username = $HTTP_COOKIE_VARS["username"];

   print '<table cellpadding="0" cellspacing="3" border="0">
      <td> • Welcome <b>'.$username.'</b>!</td>

   // activity since last visit
      if (isset($lasttime)){
         $query = mysql_query("SELECT COUNT(id) AS total FROM kokonut WHERE `date` <= NOW() AND `date` < $lasttime AND rating >= 0");
         $set = mysql_fetch_array($query);

         print '<tr><td> • There are '.$set['total'].' new Kokonuts to browse.</td></tr>';

      print '</table>';

      setcookie("username", $username, time()+3600); /*Expires in an hour*/

      } else {

      $query = mysql_query("SELECT * FROM kokonut_members WHERE username='$username' AND password='$password'");

      if (mysql_num_rows($query)){

      //set cookie for last login time
      setCookie('lasttime', time(), time()+604800);
      //set cookie for login info
      setcookie("username", $username, time()+3600); /*Expires in an hour*/

      print '<table cellpadding="0" cellspacing="3" border="0">
      <td> • You\\'re logged in!</td>

      } else {

      <table cellpadding="0" cellspacing="3" border="0">
      <td><form action="'.$PHP_SELF.'" method="post" name="submit"><img src="images/username.gif" width="36" height="15" alt="" border="0" /></td>
      <td><input type="text" name="username"></td>
      <td><img src="images/password.gif" width="35" height="15" alt="" border="0" /></td>
      <td><input type="password" name="password"></td>
      <td><input type="image" style="padding-left:7px;" name="submit" src="images/login.gif" width="46" height="19"></form></td>

The above always gives me “0” new Kokonuts. Is it because it’s reading the current time from the cookie instead of reading the users last login time?

I don’t know. Any help would be awesome.

No one?