Reputation: 9
I have a cookie problem on Chrome. I use the setcookie() function in php as a mean to automatically re-authenticate users that have already gone through an authentification process (using LinkedIn API).
Problem is, the cookies I set work fine on Firefox an IE 11 but seem to get deleted after a few seconds on Chrome. I have tested that with a bunch of users, same problem for them (I'm waiting for feedback on other browsers).
Here is the code that sets the cookies:
setcookie('CookieID', $var, (time()+365*24*3600), NULL, NULL, false, true);
setcookie('LinkedInAuth', $var, (time()+365*24*3600), NULL, NULL, false, true);
Is it a known issue ? How can I resolve this ?
EDIT 5:
I changed the topic's title as I completely reviewed my approach. (I also deleted former edits as they were irrelevant)
Instead of using the LinkedIn ID as the value for my cookie, I now generate a unique ID using openssl_random_pseudo_bytes()
function.
Here is the new code:
$random = openssl_random_pseudo_bytes(32);
$UserKey = bin2hex($random);
$expiration= time() + 365*24*3600;
setcookie('CookieID', $UserKey, $expiration, NULL, NULL, false, false);
So now, Chrome is creating a cookie with a random value, as asked, and the cookie expiration time is right. Still, it is not the correct value! I echo $UserKey
in the console and it has a diferent value from the cookie. But on FF and IE it's working like a charm!
The Response header gives me the wrong value for my cookie but the request header has the right one.
Does someone has any experience with that? Does the issue come from my code or the function I'm using?
Upvotes: 0
Views: 1007
Reputation: 9130
The following code works for me on Chrome version 52.something on Linux:
$var = '1234567890';
setcookie('CookieID', 'CK'.$var, (time()+365*24*3600), NULL, NULL, false, true);
echo 'Cookie "CookieID" should be set to "CK'.$var.'"';
exit;
I can only assume you are either not setting $var
correctly or not setting $var
before the rendering of the page (setcookie()
is a header
function and must be called before rendering the page).
Are you doing something different to the above code?
Upvotes: 1