[英]Can't retrieve data from session variable - PHP Number guessing game
I am building a number guessing game and need to create a session variable to hold the randomized target number until the user submits the correct guess. 我正在构建一个数字猜谜游戏,需要创建一个会话变量来保存随机目标数,直到用户提交正确的猜测为止。 I also need to print the number of attempts after the user submits the correct answer.
用户提交正确答案后,我还需要打印尝试次数。
I set my session variable and used a hidden field to hold the counter. 我设置了会话变量,并使用了隐藏字段来保存计数器。 I don't know if the hidden field works bc when I submit a guess, my code prints out the first if statement of the check() function..ALL THE TIME.
我不知道在提交猜测时隐藏字段是否适用于bc,我的代码会打印出check()函数的第一个if语句。
I think it has something to do with the session variable (and of course my code), but I can't figure it out. 我认为这与会话变量(当然还有我的代码)有关,但我无法弄清楚。 I've been working on this for two days now and feeling the frustrations.
我已经为此工作了两天,并感到沮丧。 Any help would be amazing.
任何帮助都将是惊人的。 Here's my full code below:
这是我的完整代码如下:
<?php session_start() ?>
<!DOCTYPE HTML>
<html>
<head>
<title>Number Guessing Game</title>
</head>
<body>
<h1>Guess the number</h1>
<p>I'm thinking of a number between 1 and 5. Can you guess what it is?<br>
In less than 3 tries?</p>
<?php
extract($_REQUEST);
error_reporting(E_ALL & ~E_NOTICE);
// check to see if this is start of game
if (filter_has_var(INPUT_POST, "guess")) {
check();
} else {
setTarget();
} //end if
// set targetNum session variable
// increment counter by 1
function setTarget() {
$targetNum = rand(1, 5);
$_SESSION["targetNum"] = $targetNum;
$counter++;
print <<<HERE
<form action="" method="post">
<input type = "text"
name = "guess">
<input type = "hidden"
name = "counter"
value = "$counter">
<h2>Target Number: $targetNum</h2>
<h3>The counter is at: $counter</h3>
<br>
<button type = "submit">
SUBMIT GUESS
</button>
</form>
HERE;
}
function check() {
global $counter;
print <<<HERE
<form action="" method="post">
<input type = "text"
name = "guess"
value= "$guess">
<input type = "hidden"
name = "counter"
value = "$counter">
<h2>Target Number: $targetNum</h2>
<h3>The counter is at: $counter</h3>
<br>
<button type = "submit">
SUBMIT GUESS
</button>
</form>
HERE;
if ($guess == $_SESSION['$targetNum']) {
print "<h3>Awesome. You guessed it in $counter attempt(s)</h3>";
unset($_SESSION["targetNum"]);
$count = 0;
print "<a href='numberGuessingGame.php'>TRY AGAIN</a>";
} else if ($guess > $_SESSION['$targetNum']) {
print "<h3>Too high. Guess again.</h3>";
} else if ($guess < $_SESSION['$targetNum']) {
print "<h3>Too low. Guess again.</h3>";
} else {
print "I don't know what that is...";
}
}
?>
</body>
</html>
Your $guess
variable is never set to the POST value (Correction: you're using extract but I'd advise against it). 您的
$guess
变量永远不会设置为POST值(更正:您正在使用数据提取,但我建议您使用它)。 You are also changing the value of your session array key when you add a '$': 添加“ $”时,您还在更改会话数组键的值:
$guess = $_POST['guess'];
if ($guess == $_SESSION['targetNum']) {
You made two basic, but severe errors. 您犯了两个基本但严重的错误。
First: DO not set the error level to exclude notices when developing! 第一:开发时请勿设置错误级别以排除提示! That way you will never spot typos in variable or array index names.
这样,您将永远不会在变量或数组索引名称中发现拼写错误。 Remove
error_reporting(E_ALL & ~E_NOTICE);
删除
error_reporting(E_ALL & ~E_NOTICE);
, or replace it with error_reporting(E_ALL);
,或将其替换为
error_reporting(E_ALL);
. 。
Second: You use extract($_REQUEST);
第二:您使用
extract($_REQUEST);
- using that function is asking for trouble. -使用该功能会带来麻烦。 PHP has a long history of security vulnerabilities because of the "register_globals" feature, which introduces global variables just because some key=value pair in the request data was parsed.
由于“ register_globals”功能,PHP引入了安全漏洞的悠久历史,该功能引入了全局变量,仅是因为解析了请求数据中的某些key = value对。 It took years to remove that feature.
删除该功能花了好几年。 You are re-implementing it without any security precaution by using that function, and with no real benefit.
您正在使用该功能在没有任何安全预防措施的情况下重新实现它,并且没有真正的好处。
Remove that extract($_REQUEST);
删除该
extract($_REQUEST);
function, and use $_REQUEST['varname']
instead of $varname
for all variables that come from the remote browser. 函数,并对来自远程浏览器的所有变量使用
$_REQUEST['varname']
而不是$varname
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.