doucheng9058
2017-10-26 13:30 阅读 108
已采纳

为什么我的cookie没有显示在链接页面上?

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条回答 默认 最新

  • 已采纳
    dpdkqls6399 dpdkqls6399 2017-10-26 13:35

    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).

    点赞 评论 复制链接分享

相关推荐