I am creating a cookie in CakePHP using setcookie() from /cake/contoller_name/method/parameter this page. But I could not read this cookie from JavaScript. However, when the above cookie sets from /cake/contoller_name/method this page, I can read this cookie from JavaScript.
So I realized we cannot set and change the cookie from the page with parameters. In the server side that in Cake we can read get these cookies, but not from JavaScript. Note: the params are passed in "cake style", not on a regular querystring; e.g.: /cake/controller/param1/param2.
Example Code
Case 1: URL: /cake/payroll/salary Page: payroll_controller.php
function salary(){
setcookie('TEST_COOKIE','TEST_DATA',0);
}
Case 2: URL: /cake/payroll/salary/14 Page: payroll_controller.php
function salary(){
setcookie('TEST_COOKIE','TEST_DATA',0);
}
Javascript Code:
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
alert(getCookie('TEST_COOKIE'));
I can read the cookie from Case 1 with JS, but not the cookie from Case 2. How can I set cookies on any page from CakePHP and read those cookies from JavaScript?