最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

I am currently having a few issues when trying to read cookies using PHP or Javascript. I have tried using:

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

And I have tried:

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

(Using the jquery.cookie plugin)

And neither of them work on iPad. It works on all browsers fine, I have tried Googling the issue but there dosen't seem to be much information on reading cookies on an iPad.

Thanks for any help you guys can give!

I am currently having a few issues when trying to read cookies using PHP or Javascript. I have tried using:

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

And I have tried:

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

(Using the jquery.cookie plugin)

And neither of them work on iPad. It works on all browsers fine, I have tried Googling the issue but there dosen't seem to be much information on reading cookies on an iPad.

Thanks for any help you guys can give!

Share Improve this question asked Sep 13, 2011 at 10:36 C BC B 4171 gold badge5 silver badges15 bronze badges 5
  • Does your code work fine on Safary (on Mac or PC)? It should work identically on iPad. – haynar Commented Sep 13, 2011 at 10:47
  • It does indeed. And Cookies are turned on on the iPad too. – C B Commented Sep 13, 2011 at 11:27
  • unfortunately I have no idea why it doesn't work, but some time ago I had problem related to cookies on all Safari version (Mac, PC, iPad etc). It was the built-in restriction of Safari (Safari doesn't accept cookies from 3rd party sites unless the user explicitly interacted with the web page). Maybe you're trying to use cookies from iframe? – haynar Commented Sep 13, 2011 at 11:39
  • Well I guess the issue isn't with the cookie technically, it is with checking if one is there or not, which seems to be the first hurdle. – C B Commented Sep 13, 2011 at 13:28
  • You're right. All the information I could find was your standard, "How to accept cookies", "How to read and write cookies", etc. In fact this question is the only thing I can find. :) Maybe you can take it up with Apple's support staff and post the answer here. – Herbert Commented Sep 16, 2011 at 10:02
Add a ment  | 

2 Answers 2

Reset to default 7 +150

This is really a workaround, but you could use the local storage if it's available - atleast I've used it in iPad/iPhone successfully.

For example with this kind of solution.

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);
}

Have you take a look at the manual regarding setcookie? You should use all variables, perhaps this solves the fact that you cannot access your cookies with JS? Also, using native JS to access the cookies (instead of jQuery), doesn't that work?

发布评论

评论列表(0)

  1. 暂无评论