C B
C B

Reputation: 417

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

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!

Upvotes: 5

Views: 2576

Answers (2)

ChrisH
ChrisH

Reputation: 1281

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?

Upvotes: 0

Marcus
Marcus

Reputation: 5143

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

Upvotes: 7

Related Questions