不能在iPad上通过PHP或Javascript读取cookie


Can't read cookies through PHP or Javascript when on an iPad

我目前在尝试使用PHP或Javascript读取cookie时遇到了一些问题。我试过使用:

if(!$_COOKIE['popup_closed']
&& !isset($_COOKIE['username'])
&& !isset($_COOKIE['password'])
)

And I have try:

if(
$.cookie('popup_closed') == null
&& $.cookie('username') == null
&& $.cookie('password') == null) {
doStuff();
}

(使用jquery。饼干插件)

它们都不能在iPad上使用。它在所有浏览器上都能正常工作,我试着在谷歌上搜索这个问题,但似乎没有太多关于在iPad上阅读cookie的信息。

谢谢你们的帮助!

这确实是一种变通方法,但如果可用,您可以使用本地存储-至少我在iPad/iPhone中成功地使用了它。

例如用这种解决方案。

function saveData(name, value) {
    if (typeof(localStorage) != 'undefined') {
        localStorage.setItem(name, value);
    } else {
        createCookie(name, value, 7);
    }
}
function loadData(name) {
    var temp_value = '';
    if (typeof(localStorage) != 'undefined') {
        temp_value = localStorage.getItem(name);
    } else {
        temp_value = readCookie(name);
    }
    return temp_value;
}
function eraseData(name) {
    if (typeof(localStorage) != 'undefined') {
        localStorage.removeItem(name);
    } else {
         eraseCookie(name);
    }
}
function createCookie(name,value,days) {
    var expires = "";
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        expires = "; expires=" + date.toGMTString();
    }
    else {
        expires = "";
    }
    document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') {
            c = c.substring(1,c.length);
        } 
        if (c.indexOf(nameEQ) === 0) {
            return c.substring(nameEQ.length, c.length);
        }
    }
    return null;
}
function eraseCookie(name) {
    createCookie(name,"",-1);
}

您看过setcookie的使用手册了吗?您应该使用所有变量,也许这解决了您无法使用JS访问cookie的事实?此外,使用本地JS访问cookie(而不是jQuery),不工作吗?