使用setcookie设置的Cookie在Chrome和Firefox中都很好用。 使用document.cookie(来自表单)设置的cookie显示为返回并重新进入Chrome中的页面,并且在Firefox中根本不显示。我在这里做错了什么?
的index.php
<?php
$value = "chocolate";
setcookie("anothercookie", $value, time() + (86400 * 30), '/');
?>
<html>
<body>
<form action="action.php" method="GET">
Name: <input type="text" name="username" /><br /><br />
Cookie: <input type="text" name="formCookie" /><br /><br />
<input type="submit" name="submit" value="Submit" />
</form>
<script>
var myJavascriptVar = document.querySelector('[name="formCookie"]').value;
document.cookie = "myJavascriptVar = " + myJavascriptVar;
</script>
</body>
</html>
action.php的
<?php
if (isset($_GET["username"]))
{
$user = $_GET["username"];
echo $user;
echo " is your username <br />";
}
else
{
$user = null;
echo "no username supplied <br />";
}
if (isset($_COOKIE['myJavascriptVar'])) {
echo "Cookie is " . $_COOKIE['myJavascriptVar'];
}
echo "<br />";
if (isset($_COOKIE['anothercookie'])) {
echo "Another cookie is " . $_COOKIE['anothercookie'];
}
?>