-5
我正在一个网站上猜测数字。网站的概要如下所示: enter image description here如何在HTML中打印出cookie值?
在setCookiesA.php,有名为c(颜色)和MAXNUM存储(猜测的最大数目)两个cookie。存储cookies后,玩家将被重定向到GuessingA.php。的代码如下所示:
<?php
setCookie("c",$_GET["itemChosen"],time()+(86400 * 365), "/");
setCookie("maxNo",$_GET["noGuess"],time()+(86400 * 365), "/");
header("Location:GuessingA.php");
?>
要GuessingA.php后,cookie值颜色应该在句子中被打印出来“你已选择的信道???为猜测”(???表示颜色)部分完成的代码如下所示。
<?php
session_start();
if (!isset($_SESSION["ans"])) {
} else {
}
$continue = true;
?>
<!DOCTYPE html>
<html>
<head>
<title> Guessing game </title>
</head>
<body>
<h1> Welcome </h1>
<p> You have chosen the $_COOKIE[c] channel for guessing</p>
<h2> The answer </h2>
<form method="post" action="<?php print htmlspecialchars($_SERVER['PHP_SELF'])?>">
<p>
Type your guess here: <input type="text" name="guess" />
<input type="submit" value="submit"
<?php if (!$continue) { print("disabled=\"disabled\""); } ?>
/>
</p>
</form>
<h2> Your guess: </h2>
</body>
</html>
我认为使用谷歌发现JavaScript函数来获取cookie的值,但没有奏效。
function readCookie(name) {
var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
var c = ca[i];
while (c.charAt(0)==' ') c = c.substring(1,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
自己创建的功能,用于打印出正文部分的颜色。
function change()
{
var color=readCookie(c);
color.innerHTML="You have chosen the " +color+" channel for guessing";
}
如何获取cookie值?非常感谢你。
我的意思是它是如此基本的和容易找到... https://www.w3schools.com/js/js_cookies.asp为什么来这里直,而不是研究,谷歌搜索? – ProEvilz
'readCookie(c);''你没有设置'c'变量,除非它在更高的范围内,并且你没有显示它(你应该) –
在我的程序中,存储了两个cookie,颜色和maxNum。因此,我想知道我是否只能获得颜色。 –