mercredi 21 décembre 2016

Pure Javascript Cookie not set

I'm trying to set a Javascript object to Cookie and get it in Server Side with PHP.

Here is my Javascript and PHP codes:

Javascript (frontend.html):

function sc(name, value, expSec) {
    var d = new Date();
    d.setTime(d.getTime() + (expSec * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = name + "=" + value + ";" + expires + ";path=/";
}
var cookieObj = {};
cookieObj['user-agent'] = userAgent.replace(/(;|,)/g, '');
cookieObj['token'] = token.value;
sc('frontend-data', JSON.stringify(cookieObj), 50);
document.location = 'http://' + document.location.host + '/backend.php;

PHP (backend.php):

$cookie = isset($_COOKIE['frontend-data']) ? $_COOKIE['frontend-data'] : null;
var_dump($cookie);

When I visits frontend.html with PC, It works correctly and output is set but when I use Mobile device, output is Null.

As I searched, I got that, there is a difference in Browsers Cookie Size. In this website, I measured my mobile browser cookie size (Chrome) and output was:

11:44:15.491: Count: Max Cookie count with Character Length 3 and character "1": 180
11:44:24.616: Size: Max Cookie Character Length using character "1": 4096
11:44:30.843: Count: Max Cookie count with Character Length 4096 and character "1": 180
11:44:36.725: Size: Max Cookie Character Length using character "ÿ": 2049
11:44:47.205: Count: Max Cookie count with Character Length 2049 and character "ÿ": 180
11:44:48.229: Count: Max Cookie count with Character Length 100 and character "1": 180
11:44:49.94: Count: Max Cookie count with Character Length 10 and character "1": 180
11:44:49.944: Count: Max Cookie count with Character Length 5 and character "1": 180
11:44:50.119: Guessing Max Cookie Count Per Domain: 180
11:44:50.120: Guessing Max Cookie Size Per Cookie: 4096 bytes
11:44:50.121: Guessing Max Cookie Size Per Domain: NA

But my Cookie size is 352 Bytes.

What is the problem?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire