Attempting to set up php quiz with limited retries using sessions

Discussing Html . Code , Software , other

Moderators: egami, macek, gesf

Post Reply
MMcnich
New php-forum User
New php-forum User
Posts: 6
Joined: Wed Apr 11, 2018 4:57 am

Wed Apr 11, 2018 5:09 am

Recently I have been trying to set up my own little quiz using php in HTML. For this quiz I have it set up so it asks a random question each time and if you get it wrong you have another chance to try again. I wanted to limit the retries to 1 retry and I know I have to use PHP sessions. I started the session at the top of the page and I attempted to make it so upon clicking submit button it would only work once then the next time it would just revert you back to the homepage of the quiz. Not only is this not working but it isn't having any affect at all on the quiz. Here is the sample code from a section of the PHP sheet that checks the answers:

Code: Select all

<?php
session_start();
include('colorquiz.php');
$choice=$_GET['choice'];
$right=@$_GET['right'];
$left=@$_GET['left'];
$which=$_GET['which'];
$correctright=$rightcolors[$choice];
$correctleft=$leftcolors[$choice];
if ($which=='right') {
    if (metaphone($left) == metaphone($correctleft)) {
       print("Correct! $correctleft is the capital of $correctright!");
       print("<p><a href='colorquizask.php'>Play again </a>");
       }   
    else {
       print("Wrong Study More!<p>\n");
       print("<a href='colorquizask.php'>Back to home </a><p>\n");
      print("OR try again: What is opposite of $correctright on the color wheel?<br>");
     print("<form action='colorquizcheck.php' method='get'>\n");
     print("<input type='text' name='left'><br>\n");
     print("<input type='hidden' name='right' value=$right>\n");
     print("<input type='hidden' name='which' value=$which>\n");
     print("<input type='hidden' name='choice' value=$choice>\n");
     print("<input type='submit' value='Submit Answer'>");
     print("</form>\n");
    } }
else {
 if (metaphone($right) == metaphone($correctright)) {
       print("Correct! The color opposite of $correctright is $correctleft!");
       $saywhich='false';
       print("<p><a href='colorquizask.php'>Play again </a>");
       }
   else {
print("Wrong Study More!<p>\n");
       print("<a href='colorquizask.php'>Back to home </a><p>\n");
      print("OR try again: $correctleft is the opposite of what color?<br>");
     print("<form action='colorquizcheck.php' method='get'>\n");
print("<input type='text' name='right'><br>\n");
     print("<input type='hidden' name='left' value=$left>\n");
     print("<input type='hidden' name='which' value=$which>\n");
     print("<input type='hidden' name='choice' value=$choice>\n");
     print("<input type='submit' value='Submit Answer'>");
     print("</form>\n");
    } }
    if( isset($_SESSION['sumbit'])){
    $_SESSION['submit'] = 1;
} else {
    if($_SESSION['submit'] =>2){
        header("colorquizask.php");
     }
    $_SESSION['submit'] += 1;
}
?>


As seen at the bottom I have it so it is at one and when it reaches 2 it will revert back to homepage. I am new to PHP and am still learning it and when looking at the documents on PHP sessions I was getting quite confused. I would appreciate any help.

Post Reply