简体   繁体   中英

Cookies don't update on some android browsers

I've developed a two-steps verification login which uses cookies to check if a device has already been verified or not: this cookie contains an unique id and a random password which changes on every access of the device. To do that update, I use this php code

setcookie('nameofthecookie', '', time()-600,"/");
setcookie('nameofthecookie', $newcookiecontent, time()+2629743,"/");

Which works perfectly on most browsers, but it does not on some android ones: infact, using them, the cookie does not update.

What I've tried to do:

  1. Only creating a cookie using

    setcookie('nameofthecookie', $newcookiecontent, time()+2629743,"/");

    and it works ;

  2. Only deleting a cookie using

    setcookie('nameofthecookie', '', time()-600,"/");

    and it works .

Anyone could suggest me a solution?

Your code leads to the following header which is sent to the client:

Set-Cookie nameofthecookie=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT; Max-Age=0; path=/ 
           nameofthecookie=somevalue; expires=Sat, 24-May-2014 22:11:13 GMT; Max-Age=2629743; path=/

Based on the RFC for HTTP Cookies (page 9), it is not defined which one of the cookies the browser will choose if the path is the same:

If multiple cookies satisfy the criteria above, they are ordered in
the Cookie header such that those with more specific Path attributes
precede those with less specific. Ordering with respect to other
attributes (eg, Domain) is unspecified.

So maybe the Android Browser chooses the cookie with the emtpy value, and your desktop browser chooses the later one. The Netscape specification for cookies states that

Instances of the same path and name will overwrite each other, with the latest instance taking precedence. Instances of the same path but different names will add additional mappings.

Actually, you do not need to reset the cookie to a blank value before setting a new value. Just remove the first line of your code.

See this tutorial for more information about managing cookies with PHP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM