简体   繁体   中英

How to extend cookie expiration time for the users

I have a webpage with a one time login. Once user login, the username will save in cookie for an year. Now, the cookie gonna expire for the users, because I'm created the webpage one year back.

Now, i want to extend the expiration time for one more year for all the existing users who lo-ginned to my page. Thanks in advance.

Below is the code which i used for saving cookie:

 $password = $_POST['password'];
 $username = $_POST['username'];

 $sql="SELECT * FROM users WHERE username='$username' and password='$password'";
 $result=mysql_query($sql);

 $count=mysql_num_rows($result);

 if($count==1){
 // Set cookies. I set my cookies to 1 year
 $expires = 60 * 60 * 24 * 365;
 setcookie("username", $username, time()+$expires);
 // Re-direct to backend
 header("location:welcome.php");
 } else {?>
 <script>alert("Wrong Username or Password!!!");
 window.location="index.php";</script>
 <?php
 }
 ?>

You should just re-save the cookie. It would look like this:

$username = $_COOKIE["username"];
if($user != "")
{
  $expires = 60 * 60 * 24 * 365;
  setcookie("username", $username, time()+$expires);
}

It would store the username of existing users for another year. Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM