简体   繁体   中英

php: $_COOKIE set-up and reading doesn't work properly

Hello i got an issue with the code I posted here (it's a very reduced version, thus many html stuff are omitted for clarity). The goal is: it checks if the cookie is present and is filled it. If the global vairable $_POST is empty, it checks if the cookie is present or is for some reason empty. In case the cookie doesn't exist or it is empty; it creates one. Then show the content before the HTML form

After the submit of the form is clicked, the page recall itself and of course the $_POST is filled in. Thus the second half of the code, reads and shows the cookie's content.

In the reality when I call the page, the cookie is created (I checked with my browser) but the first reading is NOT shown! if I click on submit, I read the cookie's content OR if I refresh the page with F5 it shows it.

But not when I load the page the "First" time.

Please could you help me to find where I make the mistake?

if (!$_POST) {
    // Create Cookie
    if (!isset($_COOKIE["ID"]) || ($_COOKIE["ID"] != true)){
     $order_id = time();
         $lifetime = 600;
     setcookie("ID",$order_id,time()+$lifetime);
    }//End IF "create cookie"
     ...
     ...
     ...
     $order_id = $_COOKIE["ID"];
     echo $pag = <<< FORM
         $order_id
     <form action="page.php" method="post">
     ...
     ...
     <input type="submit">
     </form>
FORM;
} elseif  ($_POST) {
    // Read Cookie for Contract
    echo $order_id = $_COOKIE["ID"];
    echo $order_id_date = date('l jS \of F Y');
     ...
     ...
     ...
     setcookie("ID","",-50000);
} // End IF ELSEIF

Cookies are sent by the browser to the server on each request. Because you are still in the same request the cookie hasn't been sent.

That's why further in the same file (in the same request) this doesn't work:

$order_id = $_COOKIE["ID"]; // because the cookie hasn't been sent yet.

It also looks that that isn't necessary because the variable $order_id already contains the order id.

It would be better if you set it up this way.

$order_id = time();
if(isset($_COOKIE['ID']) && !empty($_COOKIE['ID'])) {
  $order_id = $_COOKIE['ID'];
} else {
  $lifetime = 600;
  setcookie('ID', $order_id, time() + $lifetime);
}

Therefore you always set your order id and then replace it with the cookie id if its present.

I found a solution. It's a turn-around. instead of the cookie like the one I created, i used the session. it works I would like to report here the code:

session_start();
if (!$_POST) {
   if ((!isset($_SESSION['order_id'])) or $_SESSION['order_id'] != true ){
    $order_id_time = time(); // Assign variables
    $_SESSION['order_id'] = $order_id_time;
    }
$order_id_date = date('l jS \of F Y');
$order_id = $_SESSION['order_id'];
     ...
     ...
     ...
     echo $pag = <<< FORM
     $order_id
     <form action="page.php" method="post">
     ...
     ...
     <input type="submit">
     </form>
FORM;
} elseif  ($_POST) {
    // Read Cookie for Contract
echo $cook = "Order ID: ".$_SESSION["order_id"]."<br /><br />";
     ...
     ...
     ...
session_destroy();
} // End IF ELSEIF

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