dongliang1865 2015-03-24 19:16
浏览 375
已采纳

为什么我只有在刷新页面后才能使用cookie?

I run this simple example, but i see "Undefined index: color" instead of expected "red". But after I refresh the page, i see "red". So what is my mistake? Thanks.

<?php
setcookie("color","red");
echo $_COOKIE["color"];
?>
  • 写回答

1条回答 默认 最新

  • dongwen7371 2015-03-24 19:20
    关注

    The reason for this is that your setcookie function does not put the value into the $_COOKIE.

    During the refresh (when a new HTTP request is made) the $_COOKIE gets loaded with all the updated values and therefore you see it then.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?