PHP redirect no longer working with counter


Hey all, so recently I have been setting up a little quiz in PHP and it runs off of a counter. When a user gets a question right the counter goes up and displays the next question if not the counter returns to 0 and sets the user back to the first question to start over. I decided to add echos of html image code so when the user gets a question right it will display an image according to what the answer is. Doing this however broke my redirect, I basically had it set up so when it reaches the last question it redirects to a page and it was working until the addition of the images. Here is my total php code for not including the arrays as they are not important for my issue:

// current question
$currentQuestion = 0;

$currentQuestion = $_POST[“currentQuestion”];
$currentAnswer = $questionsAndAnwsers["$currentQuestion"][“answer”];

    header("Location:"); /* Redirect browser */
 }else if($_POST["guess"] == $currentAnswer){
     $guess = $_POST['guess'];
     print ("<span class='Stylize2'>Your answer: $guess <br>"); 
     print("The answer expected: $currentAnswer<br>");  
     print("Answer Correct $answerCorrect<br><br>");
     echo '<img src=MickeyMouse.png height="200"><br><br>';
     echo '<img src=Philo.jpg height="200"><br><br>';
     echo '<img src=warner.jpg height="200"><br><br>';
     echo '<img src=superman.jpg height="200"><br><br>';
     echo '<img src=own.jpg height="200"><br><br>';
     echo '<img src=scooby.png height="200"><br><br>';
     echo '<img src=garfield.jpg height="200"><br><br>';
     echo '<img src=pok1.jpg height="200"><br><br>';
     echo '<img src=peanuts.jpg height="200"><br><br>';
     echo '<img src=sam.jpg height="200"><br><br>';
     echo '<img src=haim.jpg height="200"><br><br>';
     echo '<img src=back.jpg height="200"><br><br>';
     echo '<img src=joe.jpg height="200"><br><br>';
     echo '<img src=r.png height="200"><br><br>';
     echo '<img src=wild.jpg height="200"><br><br>';
     echo '<img src=car54.png height="200"><br><br>';
     print("Next Question Below<br></span><br><br>");
   else {
     $guess = $_POST['guess'];
     print ("<span class='Stylize'>Your answer: $guess <br>");   
     print("You have failed..<br>"); 
     echo '<img src=angry.gif height="200"></span><br><br>';
      exit("Question not found!");


Are the new if statements that echo the html images now conflicting with with php redirect? It is also maybe worth noting that I did add the error checking to see if the questions and answers exist and if not to show an error message so maybe that is another potential thing conflicting with the header redirect. I would appreciate any help and if I need to post my arrays and all of my code including the base html I will.


First off, you are doing a lot of extra typing that isn’t needs and gets more complicated to add to.

[php]$images = [
1 => ‘MickeyMouse.png’,
2 => ‘Philo.jpg’,
3 => ‘warner.jpg’,
4 => ‘superman.jpg’,

if( array_key_exists($currentQuestion, $images))
echo “$images[$currentQuestion]

echo “Not found”;

The only reason a header would break, is if you output ANYTHING to the page before the header is called. Based on the code you posted, I don’t see that. That doesn’t mean that you are not doing it however. Now, a shortcut to the header redirect it to use an html redirect. But, you have some more clean up to work on first, and it is best to fix the issue if you can before you hack something to force it to work.


Thank you, yes i should have used a key array in order to form the images. While cleaning up I noticed something which is that the redirect was pointing to a question that did not exist and once I cut it down to 15 instead of 17 it successfully redirected. Since I decided to reform the images I had to count to create them for each question and that is where I realized my mistake.


Well, using the array method will make that even easier,

[php]$r = [ 1=>‘a’,2=>‘b’,3=>‘c’,4=>‘d’];
$const = 4;

if(array_key_exists($const, $r)){
if($const == key($r)) {
echo “We have arrived!”;
} else {
echo “Still more questions to go!”;

First, it verifies the key exists. Next, it checks to see if it is at the end of the array. More will need to be done, but it should give an idea of the simplification.