0

i'm learning to use cookies in PHP. I was expecting that every time i set a cookie, the cookie and all of his variables are stored on the client site so i could use them again next time the user will visit the site. Anyway in the next example (a web application with a sign in option, i use cookies to store a unique string so i could implement "Remember me" option) i can access the id of the stored cookie but the variables data seem lost. Here is example of the code i use and screenshots of what i get.

Setting up a Cookie

 if (isset($_POST['remember_me'])) {
    $token=uniqid($_SESSION['id']);
    $sql="UPDATE users SET token='$token' WHERE id='".$_SESSION['id']."'";
    $conn->query($sql);   
    setcookie("remember_me", $token, time()+30*24*60*60*1000);
}
else{
    setcookie("remember_me","",time()-1000);
} 

User page

On the user page it just simply prints out the $_COOKIE and $_SESSION array.

<?php 
    echo "SESSION: ";
    print_r($_SESSION);
?>
<br>
<?php
    echo "COOKIE: ";
    print_r($_COOKIE);
?>

Process:

What did i get wrong and why the cookies array after re-opening is empty?

EDIT:

The second time i open browser the script for seting the cookie is not executed. I just set the url to go to the user-page.php .
Examp: /localhost/MIAFormApp/script/db/HTML_PROBA/user-page.html.php

NVelichkovski
  • 31
  • 1
  • 7
  • I think this is problem `if (isset($_POST['remember_me'])) {` When it is false, it's expiring cookie. – seti Jun 07 '18 at 20:14

1 Answers1

0

Try deleting the else statement in your sample code - meaning go from:

This

if (isset($_POST['remember_me'])) {
    $token=uniqid($_SESSION['id']);
    $sql="UPDATE users SET token='$token' WHERE id='".$_SESSION['id']."'";
    $conn->query($sql);   
    setcookie("remember_me", $token, time()+30*24*60*60*1000);
}
else{
    setcookie("remember_me","",time()-1000);
} 

To this

if (isset($_POST['remember_me'])) {
    $token=uniqid($_SESSION['id']);
    $sql="UPDATE users SET token='$token' WHERE id='".$_SESSION['id']."'";
    $conn->query($sql);   
    setcookie("remember_me", $token, time()+30*24*60*60*1000);
}

When you re-open your browser, the if statement is going to check whether or not the POST variable remember_me was found. The only time that it will be found is when someone logs in because the login form is sending that information on form submit. In every other instance ( such as re-opening the browser), the else statement will be executed which isn't what you want. The reason being that setting an empty value on a cookie will delete said cookie.

LuisD
  • 50
  • 5
  • Well actually the second time i open the browser i don't execute that script. i set the url to show directly to the user-page – NVelichkovski Jun 07 '18 at 22:47
  • @NVelichkovski Ah! Gotcha. Have you tried checking whether your browser is configured to delete cookie data on browser close? ( This is an option on all major browsers as far as I know ). I tested your sample code and I'm able to see the cookie when I re-open the browser, which then suggests that the issue might not be related to your code. – LuisD Jun 07 '18 at 23:00