php:$ _ COOKIE设置和阅读无法正常工作 [英] php: $_COOKIE set-up and reading doesn't work properly

查看:60
本文介绍了php:$ _ COOKIE设置和阅读无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在这里发布的代码有问题(这是简化版本,因此为清楚起见,省略了许多html内容).目标是:它检查cookie是否存在并被填充.如果全局可变$ _POST为空,它将检查cookie是否存在或由于某种原因为空.如果cookie不存在或为空;它创造了一个.然后在HTML表单之前显示内容

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

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

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.

实际上,当我调用页面时,会创建cookie(我用浏览器检查过),但未显示第一读!如果单击提交",则会读取Cookie的内容,或者如果使用F5刷新页面,则会显示该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! 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

推荐答案

我找到了解决方案.这是一个转机.而不是像我创建的cookie一样,我使用了会话.有用我想在这里报告代码:

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

这篇关于php:$ _ COOKIE设置和阅读无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆