Home > Article > Backend Development > What should I do if the cookie setting in php is invalid?
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 ,'/');].
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);
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 ,'/');
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!