What is happening?
There are a few problems with your code. Lets have a quick look at it:
First, here is what your code will do:
- Create and display an input form (
game.php
)
- Redirect to
processGame.php
on submit and there:
- Enter a loop
- check, whether
input
equals answer
- if so, increase the local variable
$score
by 1 and
- redirect to
game.php
So here is what is going wrong:
- The reason that you do not get any score displayed, is that
$score
is only accessible within $processGame.php
.
- Once the
header()
is reached, processGame.php
will be left, and no more code will be executed. That is why you will never go further then the first iteration.
- Your loop will not take any effect. Every time you submit your
game.php
form, processGame.php
will be handled as if it would have never been opened before. So your loop will alway start at 0
(and would also run 6 times by the way! - use <5
instead of <=5
).
- You will also see the problem when entering a wrong answer. Your loop will run 6 times and then stop, leaving you with a blank page.
Well, what could you do instead?
There is a lot to work on, as I hopefully explained understandable. I can and will not provide a full code here, because this would basically mean rewrite everything you provided. But I suggest having a read on following topics and rethinking your code:
Some suggestions:
- Do not use
GET
parameters to transport the score back to the user. They can be easily manipulated (they only cheat on themselves though, do they?).
- Use Ajax calls and JavaScript to call the processing page, to not let the User reload the entire game every round.
Final thoughts on your code
In however way you rewrite your game, here is a final tip for you: Do not store your answer in a <input type="hidden">
field. The player could easily read the answer by looking at the source code. Instead, send your input factors to the processing code on the server, and let him do the work. :)
Getting you started
Here is a very basic, working solution to your problem based on your initial code. Remember, this is not how a final solution should look like, but it is intended to provide you with a starting point. Make sure you fully understand it or you will run into trouble very soon!
game.php
:
<?php
$factor1 = rand(2, 12);
$factor2 = rand(2, 12);
$score = isset($_GET["s"]) ? $_GET["s"] : 0;
$round = isset($_GET["r"]) ? $_GET["r"] : 0;
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Game</title>
</head>
<body>
<div id="stats">
<p>Your current score is: <?php echo $score; ?></p>
<?php if( isset($_GET["l"]) ): ?>
<p>Wrong answer! The correct solution would have been: <?php echo $_GET["l"]; ?>
<?php endif; ?>
<?php if( 5 == $round ): ?>
<p>Game over! You scored <?php echo $score; ?> points.
<?php endif; ?>
</div>
<div id="form">
<form method="post" action="processGame.php">
<?php echo "$factor1 * $factor2 ="; ?>
<input type="text" name="userInput" maxlength="3" />
<input type="submit" name="solve" value="Solve">
<!-- Meta information, used to compute game progress: -->
<input type="hidden" name="f1" value="<?php echo $factor1; ?>">
<input type="hidden" name="f2" value="<?php echo $factor2; ?>">
<input type="hidden" name="s" value="<?php echo $score; ?>">
<input type="hidden" name="r" value="<?php echo $round; ?>">
</form>
</div>
</body>
</html>
processGame.php
:
<?php
//retrieve postet information
$userInput = isset($_POST["userInput"]) ? intval($_POST["userInput"]) : 0;
$factor1 = isset($_POST["f1"]) ? intval($_POST["f1"]) : 0;
$factor2 = isset($_POST["f2"]) ? intval($_POST["f2"]) : 0;
$score = isset($_POST["s"]) ? intval($_POST["s"]) : 0;
$round = isset($_POST["r"]) ? intval($_POST["r"]) : 0;
// calculate correct result
$solution = $factor1 * $factor2;
//this is the url that will be returned to. Notice the use of GET parameters
$url = "game.php?r=".($round+1);
//check with user input
if( $userInput == $solution)
{
$score += 1;
$url.="&s=$score";
}
else
{
$url.="&s=$score&l=$solution";
}
//return to interface
header("Location:$url");