Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
553 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...