Forum Moderators: coopster
I can't seem to retrieve in PHP a cookie created in JS.
function setCookie(name, value, expires, path, domain, secure)
{
document.cookie= name + "=" + escape(value) +
((expires)? "; expires=" + expires.toGMTString() : "") +
((path)? "; path=" + path : "") +
((domain)? "; domain=" + domain : "") +
((secure)? "; secure" : "");
}
function check_admin(password)
{
var password_entered = prompt("Please enter your password for administrator privileges.", "");
var password = document.get_password.password.value ;
if (password_entered == password)
{
setCookie("new_cookie", "correct");
}
else
{
setCookie("new_cookie", "wrong");
}
}
<?
$new_cookie = $HTTP_COOKIE_VARS["new_cookie"];
echo $new_cookie;
?>
When I reload the page $new_cookie is empty!
Can anyone figure out why?
Thanks!
How are you calling the javascript? Is "document.get_password.password.value" defined? Chances are the javascript is failing for some reason, and the cookie is never being set.
Download Firefox, load you page, and go to Window->Javascript Console. Click the clear button. Reload your page and go through your password setting and see what, if any, errors show up in the console.
var password = document.get_password.password.value
...is defined by...
<form name="get_password" action="#">
<input type="hidden" name="password" value="<?=$password?>">
That's how I pass the password variable from php to js.
Other than that, I get no errors on the JS console of Firefox. And the cookies exist, because after setting them, I manage to retrieve and print them in the js script.