Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 977
Connor Burton Avatar asked Sep 13 '11 10:09

Connor Burton


1 Answers

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);
}
like image 190
Marcus Avatar answered Sep 30 '22 06:09

Marcus