PHP中的Cookie检测问题 [英] Problem with cookies detection in PHP

查看:160
本文介绍了PHP中的Cookie检测问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我使用的简单函数:

  public function control(){
$ string ='lolcheck';

setcookie($ string,$ string,time()+ 120,$ this-> path,$ this-> domain);

if(isset($ _ COOKIE [$ string]))return true;
else return false;
}

问题是它只在我打开页面两次时才起作用,因为它



显然每个人都建议使用这种做法,但它不适合我。



我缺少什么?

解决方案

Cookie不工作。当设置cookie时,直到下一个请求,它不可用(即存在对应的 $ _ COOKIE 键)。



实际发生的是:




  • 客户端发送请求

  • 服务器发送包含响应头字段

    此后,客户端发送cookie以及任何以下请求: / p>


    • 客户端发送包含相应 Cookie 请求头字段的请求

    • 服务器寄存器 $ _ COOKIE


    Here is the simple function that I'm using:

    public function control() {
        $string = 'lolcheck';
    
        setcookie($string, $string, time() + 120, $this->path, $this->domain);
    
        if (isset($_COOKIE[ $string ])) return true;
        else return false;
    }
    

    The problem is that it only works when I open the page twice, because it gets the previously set cookie.

    Apparently everyone suggest to use this practice, but its not working for me.

    Am I missing something?

    解决方案

    Cookies do not work that way. When a cookie is set, it is not available (i.e. a corresponding $_COOKIE key exists) until the next request.

    What actually happens is:

    • client sends a requests
    • server sends a response containing a Set-Cookie response header field

    After that the client sends the cookie along with any following request:

    • client sends a request containing a corresponding Cookie request header field
    • server registers $_COOKIE key

    这篇关于PHP中的Cookie检测问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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