WDGirlie Posted September 13, 2012 Share Posted September 13, 2012 Hello everybody! I need to create a guessing game with php using sessions. I believe I have it so far, however this is where I get confused: The directions say that I need to have a 'give up' link that shows the number of the current game. I also need to add a "Start over link that deletes the user session and use the header("location") function to navigate to the main page." I have links set up, but I am not so sure how to restart the session, how to show the answer. I am not even sure I set up the session code right. When I tried to run my page to see how I did, I come up with the error message: <i>Parse error: syntax error, unexpected $end in C:\wamp\www\Chapter9\NumberGuessingGame.php on line 47</i> This is for a class of mine, and I am a complete n00b when it comes to PHP. I understand you cannot give the answer, but I would love it if someone could lead me in the right direction. I feel I am on the right track but something is missing. Here is my code: <?php session_start() ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> <title>Number Guessing Game</title> </head> <body> <?php $RandNum = rand(0, 100); $Body = "": $counter = 0; if (isset($_POST['submit'])) { $Answer = $_POST['Answer']; if ($Answer == $RandNum) $Body .= "<p>You have guessed the correct answer!</p>"; else $Body = "<p>Incorrect! Keep trying!</p>"; ++$counter; ?> <h1>Welcome to the Number Guessing Game</h1> <h3>Directions: Enter a number between 0 and 100. Click the 'Give Up' link to see the answer. Click the 'Start Over' to play this game again!</h3> <hr /> <form action="<?php $_SERVER['PHP_SELF'] ?>" method="post"> <p>Your Guess: <input type="text" size="4" name="Answer" <?php echo SID?> /> <input type="submit" name="submit" value="Check Answer" <?php echo SID;?>/> </p> <p><?php echo $Body; ?> <!-- echo statement for incorrect and correct! --></p> <p># of Wrong Guesses: <input type="hidden" <?php echo $counter; ?> /></p> <br /> </form> <p><a href="NumberGuesingGame.php" name="giveUp" <?php echo SID;?> >Give Up</a>; <a href="NumberGuessingGame.php" name="startOver"<?php session_destroy(); header("location:NumberGuessingGame.php"); ?> >Start Over</a></p> </body> </html> I really do want to learn, and have tried researching and couldn't find anything. If someone could even just give me a site that explains sessions really well, or even tell me why I am getting my error code. Thanks, WDGirlie Quote Link to comment https://forums.phpfreaks.com/topic/268314-random-number-guessing-game/ Share on other sites More sharing options...
darkfreaks Posted September 13, 2012 Share Posted September 13, 2012 :php_tags also you are missing an ending bracket on your if statement Quote Link to comment https://forums.phpfreaks.com/topic/268314-random-number-guessing-game/#findComment-1377486 Share on other sites More sharing options...
cyberRobot Posted September 13, 2012 Share Posted September 13, 2012 You'll also want to be mindful of the semi-colon. Do you notice any issues with the following code? <?php $RandNum = rand(0, 100); $Body = "": $counter = 0; ?> FYI, the code for starting the session may work as expected. But it will break if anything gets added to that PHP code block. For example, the code breaks if you do something like <?php session_start() print 'here'; ?> Adding a semi-colon in the correct position will prevent the code from breaking. Quote Link to comment https://forums.phpfreaks.com/topic/268314-random-number-guessing-game/#findComment-1377518 Share on other sites More sharing options...
WDGirlie Posted September 13, 2012 Author Share Posted September 13, 2012 Thanks guys! Everything you said makes sense. Now I see where I missing. I feel a little silly that I always seem to have some issue that shouldn't even be an issue! Now, when I try to run the page FIREFOX comes back with " The page isn't redirecting properly Firefox has detected that the server is redirecting the request for this address in a way that will never complete. This problem can sometimes be caused by disabling or refusing to accept cookies. " But I checked my settings, and I do accept cookies. Is this FireFox or my WAMP server software? Or, could this be the break issue that cyberRobot was saying? I did add the semi-colon after the session_start() code. Thanks again, WDGirlie Quote Link to comment https://forums.phpfreaks.com/topic/268314-random-number-guessing-game/#findComment-1377569 Share on other sites More sharing options...
cyberRobot Posted September 13, 2012 Share Posted September 13, 2012 I would recommend checking out the manual entry for header(): http://php.net/manual/en/function.header.php Remember that header() must be called before any actual output is sent, either by normal HTML tags... Quote Link to comment https://forums.phpfreaks.com/topic/268314-random-number-guessing-game/#findComment-1377571 Share on other sites More sharing options...
WDGirlie Posted September 13, 2012 Author Share Posted September 13, 2012 Thank you very much for the information! I am sorry I did not look up headers before posting! Thanks for all the help, WDGirlie Quote Link to comment https://forums.phpfreaks.com/topic/268314-random-number-guessing-game/#findComment-1377607 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.