So there is such a code.

if (isset($_COOKIE['mypages'])) { header('Location: (переадресация на друг страницу)'); } else { $visit = "active"; setcookie("mypages", $visit, time() + 7 * 24 * 60 * 60, $mycook); } 

I go to the browser I look at the cookies, everything is recorded, everything is fine, there is a cookie, there is a parameter, but $_COOKIE['mypages'] doesn’t see anything next time. And they say the cook was not found.

  • And what value does $mycook ? - chernomyrdin
  • $ _SERVER ['HTTP_HOST'] - Afimida
  • And try without it. - ling
  • And in direct because of this, I just can’t understand for what reason, because this is the parameter that needs to be written in the cookie as it is related to reading I don’t understand .. - Afimida
  • Just $_SERVER['HTTP_HOST'] contains the host name, not the path to it, the documentation says that the 4th parameter is the path for which the cookie will be issued later. PS Do not forget to accept the answer - chernomyrdin

1 answer 1

Documentation on setcookie , according to her best make $mycook = '/';

 <?php define("cookie_name",'mypages'); if (isset($_COOKIE[cookie_name])) { header('Location: (переадресация на друг страницу)'); } else { setcookie( cookie_name, // Имя куки 'active', // значение time() + (7*24*60*60), // время жизни '/', // путь $_SERVER['HTTP_HOST'] // домен ); } 

But in this example, the host name can be omitted, this mode is used when you have a bunch of subdomains