为什么我提交表单时cookie值不会立即更新? [英] why the cookie value is not updated at once when i submit the form?

查看:447
本文介绍了为什么我提交表单时cookie值不会立即更新?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我提交表单时cookie值不会立即更新?

当我提交表单,输出refer_id仍然是旧的,但是当我刷新浏览器,它改变,

why the cookie value is not updated at once when i submit the form ?
when I submit the form ,the output refer_id is still the old one ,but when I refresh the browser , it change ,why can not it change at once?

<?php 
if(isset($_GET['refer_id'])){
    setcookie('refer_id',$_GET['refer_id'],time()+3600);
}
print_r($_COOKIE);
 ?>
<p>cookie refer_id:<?php echo $_COOKIE['refer_id'];?></p>
<form method="get">
    <input type="text" name="refer_id" />
    <input type="submit" value="submit" />
</form>

推荐答案

想象这样:


  1. 某些客户要求您的专页。 $ setcookie()并在标题中发送。

  2. 客户端接收到该Cookie标头, / li>
  3. 发出下一个请求时,该Cookie会随标头一起发送回服务器。

  4. 服务器会看到 cookie,将其设置为超级全局 $ _ COOKIE

  5. 您的服务器为客户端更新 em>与一个新的 setcookie()指令。

  6. 在回应之前,您读取的是输出 $ _ COOKIE 值,仍然是最后一个Cookie值。

  7. 浏览器接收新标题,更新该Cookie的值,并显示从服务器读取的旧值的内容

  1. Some client asks for your page. You setcookie() and send that in the header.
  2. That client receives that cookie header, puts it in it's cookie store.
  3. When making the next request, that cookie is sent with the header back to the server.
  4. The server sees that cookie, sets it to the superglobal $_COOKIE.
  5. Your server "updates" that cookie value for the client with a new setcookie() directive.
  6. Before responding, you read to output the $_COOKIE value which is still the last cookie value.
  7. The browser receives the new header, updates that cookie's value, and displays the content, with the old value read from the server.

setcookie()不会影响 $ _ COOKIE

冲洗,重复。

这篇关于为什么我提交表单时cookie值不会立即更新?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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