Quiz Game

'What is the capital of France?', 'options' => ['A. Paris', 'B. London', 'C. Berlin', 'D. Madrid'], 'correct' => 'A' ], [ 'question' => 'Which planet is known as the Red Planet?', 'options' => ['A. Jupiter', 'B. Mars', 'C. Venus', 'D. Mercury'], 'correct' => 'B' ], [ 'question' => 'What is 2 + 2?', 'options' => ['A. 3', 'B. 4', 'C. 5', 'D. 6'], 'correct' => 'B' ] ]; if (!isset($_SESSION['score'])) { $_SESSION['score'] = 0; $_SESSION['current_question'] = 0; $_SESSION['answers'] = []; } // Process form submission if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_POST['answer'])) { $current = $_SESSION['current_question']; $answer = sanitizeInput($_POST['answer']); // Store answer $_SESSION['answers'][$current] = $answer; // Check if answer is correct if ($answer === $questions[$current]['correct']) { $_SESSION['score']++; } // Move to next question $_SESSION['current_question']++; } // Reset quiz if (isset($_POST['reset'])) { $_SESSION['score'] = 0; $_SESSION['current_question'] = 0; $_SESSION['answers'] = []; } // Function to sanitize input function sanitizeInput($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> Online Quiz App

Online Quiz

Question of :

Quiz Completed!

Your Score: out of

Your Answers:

$q): ?>

Question :
Your answer:
Correct answer:

Comments

Popular posts from this blog

C-programming Introduction (Notes).

My Resume / Achievements.

All about hacking.