Home > Backend Development > PHP Tutorial > php setcookie函数失灵

php setcookie函数失灵

WBOY
Release: 2016-06-13 12:07:33
Original
1002 people have browsed it

php setcookie函数失效

最近在为一个网站写php后台的时候发现setcookie这个函数总是不能正常的设置cookie,通过var_dump()发现函数返回值是false,显然是设置不成功,谷歌之,发现setcookie失效可能有这样一些原因:

  1. 浏览器禁用了cookie。不用检查,我肯定没有禁用。
  2. 使用php的setcookie()来创建cookie和php中开启session的 session_start()一样,在setcookie()之前不能有任何HTTP头部输出。我去检查代码,发现如下代码

    	<code class="php">var_dump(<span class="variable">$qOpenId</span>);setcookie(<span class="string">'qOpenId'</span>,<span class="variable">$openid</span>,time() + <span class="number">30</span> * <span class="number">24</span> * <span class="number">3600</span>,<span class="string">'/'</span>);</code>
    Copy after login
    果断注释var_dump($OpenId)这行,测试,成功。

个人认为,setcookie应该是直接设置http header里面的内容,所以前面不能有任何头部输出,var_dump,echo 什么的都不能有

php setcookie函数失效

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template