简体   繁体   English

php:$ _ COOKIE设置和阅读无法正常工作

[英]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). 您好,我在这里发布的代码有问题(这是简化版本,因此为清楚起见,省略了许多html内容)。 The goal is: it checks if the cookie is present and is filled it. 目标是:它检查cookie是否存在并被填充。 If the global vairable $_POST is empty, it checks if the cookie is present or is for some reason empty. 如果全局可变$ _POST为空,它将检查cookie是否存在或出于某种原因为空。 In case the cookie doesn't exist or it is empty; 如果cookie不存在或为空; it creates one. 它创造了一个。 Then show the content before the HTML form 然后在HTML表单之前显示内容

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. 单击表单的提交后,页面会重新调用自身,当然还会填充$ _POST。因此,代码的后半部分读取并显示cookie的内容。

In the reality when I call the page, the cookie is created (I checked with my browser) but the first reading is NOT shown! 实际上,当我调用该页面时,就创建了cookie(我用浏览器检查过),但是没有显示第一个读数! if I click on submit, I read the cookie's content OR if I refresh the page with F5 it shows it. 如果单击“提交”,则会读取Cookie的内容,或者如果使用F5刷新页面,则会显示该Cookie。

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. 浏览器会根据每个请求将Cookie发送到服务器。 Because you are still in the same request the cookie hasn't been sent. 由于您仍在同一请求中,因此尚未发送Cookie。

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. 它看起来也没有必要,因为变量$order_id已包含订单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. 因此,您始终要设置订单ID,然后将其替换为Cookie ID(如果存在)。

I found a solution. 我找到了解决方案。 It's a turn-around. 这是一个转变。 instead of the cookie like the one I created, i used the session. 而不是像我创建的cookie一样,我使用了会话。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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