Read a PHP cookie from JavaScript

PHP:

You can read cookie which created with php.

You only need to add ‘/’ (path)!!

setcookie(“username”, $username, $expire, ‘/’);

 

Js:

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;

}

 

discussion: http://stackoverflow.com/questions/8001676/read-a-php-cookie-from-javascript

B logbook : PHP | Javascript | Laravel | CodeIgniter | VueJs | ReactJs | WordPress

Translate »