user2372469
user2372469

Reputation:

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
 }
 ?>

Upvotes: 1

Views: 3704

Answers (1)

user3529020
user3529020

Reputation: 86

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.

Upvotes: 1

Related Questions