setcookie()不会在Google Chrome中设置Cookie [英] setcookie() does not set cookie in Google Chrome

查看:1589
本文介绍了setcookie()不会在Google Chrome中设置Cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在浏览一些关于如何设置cookie的PHP教程。我注意到,在FF4和IE9上成功设置了cookies,但是它并未在Chrome(11.0.696.60)中设置。 PHP文件是从XAMPP(localhost)提供的。



我试过w3schools的例子:

 <?php 
setcookie(user,Alex Porter,time()+ 3600);
?>

本站(适用于localhost环境):

 < ?php 
setcookie(username,George,false,/,false);
?>

预先致谢。

解决方案

为IP地址和 localhost 禁用cookie是设计决定。另请参阅: https://code.google.com/p/chromium/问题/详细信息?id = 56211



解决此问题的方法包括:


  • 设置本地域(例如,编辑 / etc / hosts 以使用 127.0.0.1 localhost.com )。

  • 使用 http://myproject.localhacks.com/(它指向127.0.0.1)。
  • 在设置cookie时使用空的域值。


    例如,在 PHP 中:

      setcookie(
    $ AUTH_COOKIE_NAME,
    $ cookie_value,
    time()+ cookie_expiration(),
    $ BASE_DIRECTORY,
    null,
    false,
    true
    );

    这里的值 null 表示域请勿设置。



    注意:不设置域可防止cookie对子域可见。


    I am going through some PHP tutorials on how to set cookies. I have noticed that cookies are successfully set on FF4 and IE9, however it does not get set in Chrome (11.0.696.60). The PHP file was served from XAMPP (localhost).

    I tried the example from w3schools:

    <?php
    setcookie("user", "Alex Porter", time()+3600);
    ?>
    

    And from this site (for localhost environments):

    <?php
    setcookie("username", "George", false, "/", false);
    ?>
    

    Thanks in advance.

    解决方案

    Disabling cookies for IP addresses and localhost was a design decision. See also: https://code.google.com/p/chromium/issues/detail?id=56211

    Ways to work around the issue include:

    • Set a local domain (e.g., edit /etc/hosts to use 127.0.0.1 localhost.com).
    • Use http://myproject.localhacks.com/ (which points to 127.0.0.1).
    • Use an empty domain value when setting the cookie.

    For example, in PHP:

    setcookie(
      $AUTH_COOKIE_NAME,
      $cookie_value,
      time() + cookie_expiration(),
      $BASE_DIRECTORY,
      null,
      false,
      true
    );
    

    Here the value null indicates that the domain should not be set.

    Note: not setting the domain prevents the cookie being visible to sub-domains.

    这篇关于setcookie()不会在Google Chrome中设置Cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆