PHP cookie not working

I am trying to access a cookie that I just set on another page in the same domain, but it does not work. When I do echo $_COOKIE , the array is empty on the new page, but contains a cookie on the create page.

Here is the code in /PROC/LOGIN.PROC.PHP

 //Set the cookie for 1 year. setcookie("username", $username, time()+365*24*60*60); setcookie("password", $password, time()+365*24*60*60); 

Here is the code in /INC/HEADER.INC.PHP

 if (isset($_COOKIE['username']) && isset($_COOKIE['password'])) { include("pages/user.header.pages.php"); 

But when I try to display a cookie or display only the array in header.inc.php, the array is empty.

+4
source share
1 answer

You need to set the path value of the cookie to the root of your domain, as per the docs :

 setcookie("username", $username, time()+365*24*60*60, '/'); 

Otherwise, it will be installed in the current working directory, which is /PROC/ for your example. Thus, only scripts in /PROC/ could use this cookie.

+11
source

Source: https://habr.com/ru/post/1437253/


All Articles