php关于cookie的一些问题

来源:互联网 发布:聊城职业技术学院网络 编辑:程序博客网 时间:2024/05/14 15:43

今天遇到一个问题  折腾了我一会儿!

原来同一页面 先 setcookie ,马上获取 cookies ,是得不到的!第二次载入此页面才可以得到。可以立即到别的页面得到。

在官方网站看到setcookie是这样解释的:

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

也就是说,setcookie是通过HTTP请求响应的Header来完成的,需要在请求响应内容输出之前执行。

但是我今天测试的时候,在setcookie之前有echo输出,还是设置成功了,不知道什么原因。


今天又发现一个问题:通过ajax获取不到cookie变量的值!