Home > Backend Development > PHP Problem > What should I do if the cookie setting in php is invalid?

What should I do if the cookie setting in php is invalid?

王林
Release: 2023-03-11 08:48:01
Original
2166 people have browsed it

The solution to the invalid setting of cookies in php is to add a fourth parameter when using the setcookie function to ensure that it takes effect in all directories, such as [setcookie("id",$id, time() 36002430 ,'/');].

What should I do if the cookie setting in php is invalid?

The operating environment of this article: windows10 system, php 7.3, thinkpad t480 computer.

We usually use the setcookie function to set cookies on a page, such as

setcookie("id",$id, time()+36002430);
Copy after login

But after returning to the homepage, you may find that it does not take effect. There is no data in $_COOKIE in PHP.

After that, I went to the official website to check the instructions of setcookie, and finally found that the fourth parameter of setcookie is path. That is to say, if the fourth parameter is empty, it will only take effect in the current directory by default. Under normal circumstances, there is no problem.

But my site is set up with rewrite and index.php is hidden, so the set cookie is only valid on this page.

The solution is to add the fourth parameter, such as

setcookie("id",$id, time()+36002430 ,'/');
Copy after login

Related learning video sharing: php video tutorial

The above is the detailed content of What should I do if the cookie setting in php is invalid?. For more information, please follow other related articles on the PHP Chinese website!

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