Cookies not saving on Android mobile

左心房为你撑大大i 提交于 2020-01-06 15:49:37

问题


I'm using this javascript function to setup a cookie on my site that will show a div wile the cookie isn't setup.

function accept_cookies(){
    days=365; // number of days to keep the cookie
    myDate = new Date();
    myDate.setTime(myDate.getTime()+(days*24*60*60*1000));
    document.cookie = 'cookie_t=1; expires=' + myDate.toGMTString();
}

usage:

<a onClick="HideContent('cookies'); accept_cookies()" href="javascript:HideContent('cookies')">
    ok
</a>

This method works great on my desktop device but, on my android phone it doesn't work so well. I know this because I click the link several times and the div keeps on showing after it the link saying that is ok.


回答1:


You can use the php method "setcookie" to create a Set-Cookie header, that will set a cookie on the client side.

http://php.net/manual/en/function.setcookie.php

Processes that happens in the server side (php) are much more reliable than on the client side.

Usage example with php and ajax jquery library:

<a href="#" onClick="ajaxPost()">active</a>

function ajaxPost(){
   $.post("action.php",{action: "setTheCookie"}, function(result){
    // Do something
   }
}

action.php

checkAction();
function checkAction(){
   if($_POST["action"] == "setTheCookie")
     setCookie("cookie name", "cookie value", time() + (86400 * 30), "/");
   exit();
}


来源:https://stackoverflow.com/questions/38675212/cookies-not-saving-on-android-mobile

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!