当你打开3.php后,那么会在你机器的网页临时文件夹里面写入cookie,就算你不关闭,重新打开4.php应该也会显示“王亚”cookie是保存在客户端的,并不是服务器端。参考:本人的机器操作系统是windows server 2008,网页临时文件的目录是:C:\Users\Administrator\AppData\Local\Microsoft\Windows\Temporary Internet Files

解决方案 »

  1.   

    cookie是保存在客户端的.
    因为你用setcookie函数没有指明cookie的生存周期,所以导致你再打开一个游览器,就查看不到cookie.setcookie("name","王亚", time()+3600);改成这样就可以了,cookie一小时
      

  2.   

    默认的情况只在当前页面有效,为一次连接的生命周期,这点同session是一样的.
    也有人称这种情况为session cookie.
      

  3.   

    PHP手册上说得很清楚:
    Cookies will not become visible until the next loading of a page that the cookie should be visible for. To test if a cookie was successfully set, check for the cookie on a next loading page before the cookie expires. Expire time is set via the expire  parameter. A nice way to debug the existence of cookies is by simply calling print_r($_COOKIE);. 就是说写setcookie的页不再次调用页面的话COOKIE是不能被访问的.一般为了回避这个问题在setcookie的页面写redirect
    <?php
    setcookie("xxx","XXX");
    header("Location: 4.php);
    ?>