Home > Backend Development > PHP Tutorial > php setcookie function fails

php setcookie function fails

巴扎黑
Release: 2023-03-01 09:06:02
Original
2064 people have browsed it

Recently, when I was writing a php backend for a website, I found that the setcookie function could not always set cookies normally. Through var_dump(), I found that the return value of the function was false, which obviously means that the setting was unsuccessful. After searching Google, I found that the setcookie failure may be caused by something like this. Reason:

1. The browser has disabled cookies. No need to check, I'm sure it's not disabled.

2. Using php's setcookie() to create cookies is the same as session_start() to open a session in php. There cannot be any HTTP header output before setcookie(). I checked the code and found the following code

var_dump($qOpenId);
setcookie('qOpenId',$openid,time() + 30 * 24 * 3600,'/');
Copy after login

decisively commented the line var_dump($OpenId), the test was successful.

Personally, I think that setcookie should directly set the content in the http header, so there can’t be any header output in front, var_dump, echo, etc.

Related articles:

Please tell me if setcookie does not set the expiration time. How to write the set path?

PHP setcookie usage

cookies settings PHP setcookie settings Cookie usage and invalid settings

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