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.
If the server doesn't allow credentials being sent along, the browser will just not attach cookies and authorization headers. So this could be another reason why the cookies are missing in the POST cross-site request.
The setcookie() function defines a cookie to be sent along with the rest of the HTTP headers. A cookie is often used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too.
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:
/etc/hosts
to use 127.0.0.1 localhost.com
).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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With