I do my development and testing in a Xampp environment, running all the code on the localhost. But for some reason, I seem to be unable to set cookies in that environment. When I look at cookies for 127.0.0.1, there are cookies there (that were set by other pages that I didn't write), so (I think) I know they
can be set... I don't know what I'm doing wrong!
Here's a simple cookietest page I did...
- PHP: Select all
<?php
if (isset($_POST['initials']) && isset($_POST['zipcode'])) {
setcookie('initials', $_POST['initials'], time()+(180*24*3600));
setcookie('zipcode', $_POST['zipcode'], time()+(180*24*3600));
$redirect="cookietest2.php";
header("Location: $redirect");
} else {
echo "
<form action='{$_SERVER['PHP_SELF']}' method='POST'>
<p>Please type your initials: <input type='text' name='initials' /></p>
<p>Please enter your zipcode? <input type='text' name='zipcode' /></p>
<p><input type='submit' /></p>
</form>
";
}
?>
And here's the page it redirects to.
- PHP: Select all
<?php
if (isset($_COOKIE['initials'])) {
echo "You've got cookies!<br />The initials cookie
is set to ".$_COOKIE['initials']."<br />";
} else {
echo "Your browser did not accept the initials cookie.<br />";
}
if (isset($_COOKIE['zipcode'])) {
echo "You've got cookies!<br />The zipcode cookie
is set to ".$_COOKIE['zipcode']."<br />";
} else {
echo "Your browser did not accept the zipcode cookie.<br />";
}
?>
This fails to set cookies and returns the "Your browser did not accept" messages.
I can create cookies in a text file and import them (using
CookieSafe) and then I can read them (not sure if I can write/update them) but if I need to set a cookie dynamically (e.g. the session ID), I can't do it.
Update - I just tested and I can NOT update cookies set by importing via CookieSafe. That makes these cookies pretty useless.