淑昊柠 发表于 2017-4-6 10:57:15

php关于cookie的一些问题

  今天遇到一个问题 折腾了我一会儿!
  原来同一页面 先 setcookie ,马上获取 cookies ,是得不到的!第二次载入此页面才可以得到。可以立即到别的页面得到。
  在官方网站看到setcookie是这样解释的:
  setcookie()definesa cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sentbeforeanyoutput 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>tagsas well as any whitespace.

  也就是说,setcookie是通过HTTP请求响应的Header来完成的,需要在请求响应内容输出之前执行。
  但是我今天测试的时候,在setcookie之前有echo输出,还是设置成功了,不知道什么原因。
  

  今天又发现一个问题:通过ajax获取不到cookie变量的值!
  
页: [1]
查看完整版本: php关于cookie的一些问题