Why my cookie not showing up on linked page?

陌路散爱 提交于 2021-02-05 09:28:40

问题


very simple question.

This is my index.php:

<?php
setcookie("testcookie", "i am value of cookie", 86400, '/');

?>

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>

<a href="newpage.php">go to next</a>
</body>
</html>

this is my newpage.php:

<?php
echo "ok ok ";
echo $_COOKIE["testcookie"];
?>

My second page echoes "ok ok " but it doesn't echo the cookie. I checked network, the index.php has the cookie "testcookie" but newpage.php doesn't have access to it if I click the link.

I'm using MAMP.


回答1:


86400 is a long, long time ago in the past.

Your cookie expires immediately.

See the documentation:

The time the cookie expires. This is a Unix timestamp so is in number of seconds since the epoch. In other words, you'll most likely set this with the time() function plus the number of seconds before you want it to expire. Or you might use mktime(). time()+60*60*24*30 will set the cookie to expire in 30 days. If set to 0, or omitted, the cookie will expire at the end of the session (when the browser closes).



来源:https://stackoverflow.com/questions/46955733/why-my-cookie-not-showing-up-on-linked-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!