首次访问时未设置Cookie [英] Cookie is not set on the first visit

查看:93
本文介绍了首次访问时未设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管我已经使用 setcookie 设置了cookie,但为什么在第一次时却跳到了 else 部分执行/首次访问?

Though I have set the cookie using setcookie, why does it skip to the else part during the first time of execution/ first visit?

<?php
    setcookie("dan", "Sony", time()+60);
    if(isset($_COOKIE['dan']))
    {
        echo "Set";
    }
    else
    {
        echo "Not yet!";
    }

?>

PS:我知道这是一个幼稚的问题,会被否决,但我找不到更好的论坛比StackOverflow。

P.S: I know it is a naive question and gets downvoted but I don't find a better forum than StackOverflow.

推荐答案

setcookie()仅安排PHP脚本发出的HTML标头包含必要的 Set-Cookie:标头。浏览器通过存储cookie,然后在对站点的下一个请求中重新对其进行响应。

setcookie() merely arranges for the HTML headers emitted by the PHP script to contain the necessary "Set-Cookie:" header. The browser responds by storing the cookie and then regurgitating it on the next request to the site.

setcookie()不会在当前执行的脚本内设置任何变量,这就是为什么您第一次看不到任何东西的原因。

setcookie() does not set any variables inside the currently-executing script, which is why you're not seeing anything the first time through.

这篇关于首次访问时未设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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