Home » Php » php – setcookie() does not set cookie in Google Chrome

php – setcookie() does not set cookie in Google Chrome

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

Domain should be equal to NULL.

& Should be given an expiry date. i.e.,

setcookie("username", "George", time() + (20 * 365 * 24 * 60 * 60), "/", NULL);

Answer:

It seems like this might be a bug with the “Developer Tools” feature of Chrome. The whole time I was trying to set a cookie (but not retrieve it) and it worked with the other browser. It worked, assuming you trust the cookie viewing section of FF or locate the cookie’s file for IE. In Chrome I was relying on the “Cookies” section of the “Developers Tools” (Developer Tools > Resources > Cookies).

I decided to take a step further and actually output the cookie’s value using this script found in WHT (posted by Natcoweb):

<?php
setcookie('test', 'This is a test', time() + 3600);
if(isset($_COOKIE['test'])){
$cookieSet = 'The cookie is ' . $_COOKIE['test'];
} else {
$cookieSet = 'No cookie has been set';
}
?>

<html>
<head><title>cookie</title></head>
<body>

<?php
echo $cookieSet;
?>

</body>
</html>

And it worked on all browsers including Chrome (I get: “The cookie is This is a test”)! However Chrome’s cookie inspector continues showing “This site has no cookies”. I also managed to find the list of cookies stored in Chrome’s settings (Options > Under the Hood > Content Settings > All cookies and site data) and finally found the cookie (more steps to check but at least more accurate than developer tools)!

Conclusion: cookies were being set, but Chrome’s development tools can’t see it for some reason.

Answer:

Did you check your system date?
$ date
And if it is old time then you should change your time
$ date -s 2007.04.08-22:46+0000

I hope this helps. I had the same problem and it worked

Answer:

I faced the same issue when i tried as below

setcookie("gb_role",base64_encode($_SESSION["role"]),time()+60*60*24*30);

when i changed it to below

setcookie("gb_role",base64_encode($_SESSION["role"]),time()+2592000);

I just worked fine, the difference is instead of time()+60*60*24*30 i just did time()+some numeric value work. I know that doesn’t make sense but it worked.

Answer:

This code is working for me in IE, Chrome and FF

if($_COOKIE['language']==NULL || empty($_COOKIE['language']))
{


    $dirname = rtrim(dirname($_SERVER['PHP_SELF']), '/').'/';

    $expire=time()+31536000;


    setcookie("language", "English",$expire,"$dirname","mydomain.com",false,false);
}