Reputation: 3949
I was trying to make a hangman game with php add I got the whole thing to work, execpt that if you run out of guesses it doesn't redirect to fail.php. The weird thing is that is does redirect if you win. I don't know whats wrong this is my quess_letter.php file:
<?php
ob_start();
$letter = $_POST['letter'];
if (preg_match('/^[a-zA-Z]$/', $letter)) {
$guesses = intval(substr($_COOKIE['hangman'], 0, 1));
$word = explode(" ", $_COOKIE['hangman_word']);
if (!strpos($_COOKIE['hangman_word'], $letter) && $letter != substr($_COOKIE['hangman_word'], 0, 1)) {
$guesses -= 1;
if ($guesses == 0) {
header("location: fail.php");
}
}
$i = 0;
foreach(str_split($word[0]) as $l) {
if ($l == $letter) {
$word[1][$i] = $l;
}
$i++;
}
$new_cookie = strval($guesses) . " " . substr($_COOKIE['hangman'], 1) . $letter . " ";
setcookie('hangman', $new_cookie, time() + 3600 * 24);
setcookie('hangman_word', "$word[0] $word[1]", time() + 3600 * 24);
if ($word[0] != $word[1]) {
header("location: index.php");
} else {
header("location: win.php");
}
} else {
header("location: index.php?error=true");
}
?>
This is the whole file. It's supposed to redirect when $guesses is equal to 0. And theres nothing being printed before the redirect. I've researched it and none of the answers worked. Could someone explain what is wrong? Thanks
Upvotes: 0
Views: 186
Reputation: 5038
if your are sure that $guesses will get to '0' exactly then
try
if ($guesses == 0)
{
header("location: fail.php");
exit();
}
Upvotes: 1