Jump to content

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/268314-random-number-guessing-game/
Share on other sites

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.

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.