Cookie无法保存在Android移动设备上 [英] Cookies not saving on Android mobile
问题描述
我正在使用此javascript函数在我的网站上设置Cookie,该Cookie会显示未设置Cookie的div.
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();
}
用法:
<a onClick="HideContent('cookies'); accept_cookies()" href="javascript:HideContent('cookies')">
ok
</a>
此方法在我的桌面设备上效果很好,但是在我的Android手机上效果不佳.之所以知道这一点,是因为我多次单击了该链接,而div继续在该链接之后显示该链接说没问题.
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.
推荐答案
您可以使用php方法"setcookie"创建Set-Cookie标头,该标头将在客户端设置Cookie.
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
在服务器端(php)发生的进程比在客户端可靠得多.
Processes that happens in the server side (php) are much more reliable than on the client side.
带有php和ajax jQuery库的用法示例:
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();
}
这篇关于Cookie无法保存在Android移动设备上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!