Trouble with a hangman game

I am trying to make a PHP hangman game where the game loads the particular word (random), the user’s progress, and wrong guesses from files into arrays. After each guess the arrays are put back into files so they can be read again. There are three files that the game is passed through. Index.php for logging in, startgame.php for the first guess and to set up the files and such, and game.php to loop through the rest of the game until the user runs out of guesses or wins.

A session.txt file is created for each user, with the first line containing a number (representing the word file to open for the game). The following lines vary depending on the length of the word in the word file. For each letter in the word file, an underscore ( _ ) is placed in the session.txt file. These are replaced as the user guesses correctly.

The problem I’m having is during the pass to game.php (last file to loop through). The underscores are visible in the session.txt file when the user is on startgame.php. As soon as they guess and are redirected to game.php, the underscores are no longer in the session.txt file. The game (as it is from the files below) can be found here.

index.php (no php involved)
[php]

Welcome to Hangman! Log in or create a new session. User accounts are case sensitive.

Username:
Key: This is a new account.
[/php] startgame.php [php] <?php

if ( !($_POST[“username”]))
header( ‘Location: index.php’ );

//Create a cookie to time the game…
setcookie(“DBXhangman”, $_POST[“username”], time()+300);

$keyfile = “users/” . $_POST[“username”] . “/key.txt”;
$sessionfile = “users/” . $_POST[“username”] . “/session.txt”;
$sessionerrors = “users/” . $_POST[“username”] . “/sessionerrors.txt”;
$moneyfile = “users/” . $_POST[“username”] . “/money.txt”;

if (file_exists($keyfile))
{
$stored_key = file_get_contents($keyfile);
$stored_money = file_get_contents($moneyfile);

if ($stored_key == $_POST[“key”])
{
echo 'Logged in as: ’ . $_POST[“username”] . ‘.
’;
}
else
echo ‘Your key was incorrect.’;
}
else
{
if ($_POST[“newaccount”])
{
mkdir(“users/” . $_POST[‘username’]);
file_put_contents($moneyfile, “0”);
file_put_contents($keyfile, $_POST[“key”]);
echo ‘Account created.’;
}

else
echo 'That username does not exist.';

}

//We assume the user logged in successfully…

if (file_exists($sessionfile))
{
unlink($sessionfile);
}

if (file_exists($sessionerrors))
{
unlink($sessionerrors);
}

$i = 0;
$j = 0;
while ( file_exists(“words/” . $j . “.txt”) )
{
$j = $i;
$i++;
}

$j–;

$randomfile = rand(0, $j);

$openfile = file(“words/” . $randomfile . “.txt”);

$lettercount = countLines(“words/” . $randomfile . “.txt”);

//Create the session file with the random file number at the top and the hidden letters as underscores…
$file = fopen($sessionfile, “x+”);

//Create the session errors file…
$file2 = fopen($sessionerrors, “x+”);

file_put_contents($sessionfile, $randomfile);

file_put_contents($sessionerrors, “0”); //This is the amount of errors made…

for ($i = 0; $i < $lettercount; $i++)
{
file_put_contents($sessionfile, “\n_”, FILE_APPEND);
}

//Start displaying the game now…

echo $randomfile . ‘.txt.
’;

$sessionarray = file($sessionfile);

$letterindex = 1;

while ($letterindex < $lettercount+1)
{
echo $sessionarray[$letterindex];
$letterindex++;
}

echo ‘

’;

//We’ll send the user to the game.php file to finish the game…
echo ’
Guess:

';

function countLines($filepath)
{

$handle = fopen( $filepath, “r” );

$count = 0;

while( fgets($handle) )
{
$count++;
}

fclose($handle);

return $count;
}
?>
[/php]
game.php
[php]

<?php if ( !(isset($_COOKIE["DBXhangman"]))) header( 'Location: index.php' ); //This way we make sure everything is ok before we begin... //Gather the data we need by calling the session... $keyfile = "users/" . $_COOKIE["DBXhangman"] . "/key.txt"; $sessionfile = "users/" . $_COOKIE["DBXhangman"] . "/session.txt"; $sessionerrors = "users/" . $_COOKIE["DBXhangman"] . "/sessionerrors.txt"; $moneyfile = "users/" . $_COOKIE["DBXhangman"] . "/money.txt"; $stored_key = file_get_contents($keyfile); $stored_money = file_get_contents($moneyfile); $session_array = file($sessionfile); $sessionerror_array = file($sessionerrors); $word_file = "words/" . $session_array[0] . ".txt"; $word_file_array = file($word_file); /* //If we need to test if the cookie is set, uncomment... if ( isset($_COOKIE["DBXhangman"]) ) echo '
Hangman cookie set as ' . $_COOKIE["DBXhangman"]; */ //Compare the guess with the actual answers... $guess = $_POST["guess"]; $lettercount = countLines($word_file); //Make a duplicate session file array to compare them and see if the user gets a point against them... $session_array_clone = $session_array; for ($i = 0; $i < $lettercount; $i++) { if ($guess == $word_file_array[$i]) $session_array[$i+1] = $word_file_array[$i]; } //Edit the session error array if the user got the guess wrong... $lettercount_errors = countLines($sessionerrors); if ($session_array == $session_array_clone) { $sessionerror_array[0]++; $sessionerror_array[$lettercount_errors + 1] = $guess; $lettercount_errors++; } //Now, before we display the game: add the arrays to their propper files... file_put_contents($sessionfile, $session_array[0]); for ($i = 1; $i < $lettercount; $i++) { file_put_contents($sessionfile, $session_array[$i], FILE_APPEND); file_put_contents($sessionfile, "\n", FILE_APPEND); } file_put_contents($sessionerrors, $sessionerror_array[0]); for ($i = 1; $i < $lettercount_errors; $i++) { file_put_contents($sessionerrors, $sessionerror_array[$i], FILE_APPEND); file_put_contents($sessionerrors, "\n", FILE_APPEND); } //Now let's display the user's progress with the word... for ($i = 1; $i < $lettercount + 1; $i++) { echo $session_array[$i]; } echo '

'; //We'll send the user to the game.php file to continue the game... echo ' Guess: '; function countLines($filepath) { $handle = fopen( $filepath, "r" ); $count = 0; while( fgets($handle) ) { $count++; } fclose($handle); return $count; } ?>

[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service