setcookie() question
Posted: Tue Jul 20, 2004 9:09 am
If i set a cookie in php using setcookie(), how do you reference that cookie and put it's value into a javascript variable using javascript?
A community of PHP developers offering assistance, advice, discussion, and friendship.
http://forums.devnetwork.net/
Code: Select all
function setCookie(cookiename, cookievalue){
document.cookie = cookiename + '=' + escape(cookievalue);
}Code: Select all
function fetch_cookie(cookie_name){
js_cookie = document.cookie.split("; ");
for(i = 0; i < js_cookie.length; i++){
js_crumb = js_cookieїi].split("=");
if(js_crumbї0] == cookie_name) {
return unescape(js_crumbї1]);
}
}
}Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
I can't seem to make the script work. It keeps returning undefined. Here's how I'm using it. First the setcookie() on page 1.Code: Select all
setcookie("username", "Gene", 0, "/", "somesite.com", 0);
And the javascript
function fetch_cookie(cookie_name){
js_cookie = document.cookie.split("; ");
for(i = 0; i < js_cookie.length; i++){
js_crumb = js_cookieїi].split("=");
if(js_crumbї0] == cookie_name) {
return unescape(js_crumbї1]);
}
}
}
document.write(fetch_cookie("username"));Code: Select all
andCode: Select all
tags where appropriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]Code: Select all
setCookie('name', 'liljester');Code: Select all
setCookie('name', 'liljester','time()+3600','/');