First of all, I just started PHP. I know my methods might not be the most efficient, but please try to remember that I'm new to all of this. Examples work great.
I'm trying to write a simple php code for that asks the user three questions. One true/false, one multiple choice and one short answer. Each question is displayed on its own page.
When I try to run this, I get the following error:
Error: Deprecated: Function session_is_registered() is deprecated on line 10
This script still runs with this error. The problem though is at the end of the quiz, the variable $correct does not seem to change. I printed the variable to see if it was changing and nothing gets printed so obviously something is wrong -- possibly the deprecated function error is the culprit.
Any help would be appreciated.
<?php
ini_set('session.gc_maxlifetime',900);
//echo ini_get("session.gc_maxlifetime");
session_start();
if($_SESSION['loggedin'] !== 1) {
header('Location: login.php');
exit;
}
if (!session_is_registered("loggedin"))
{
$_SESSION["number"] = 0;
$_SESSION["correct"] = 0;
}
$total_number = 3;
print <<<TOP
<html>
<head>
<title> History Quiz </title>
</head>
<body>
<h3> History Quiz </h3>
TOP;
$number = $_SESSION["number"];
$correct = $_SESSION["correct"];
if ($number == 0){
print <<<FIRST
<p> You will be given $total_number questions in this quiz. <br /><br/>
You will have 15 minutes to complete it. <br /><br/>
You cannot go back to change previous answers.<br /><br/>
Here is your first question: <br /><br />
</p>
<p>1. Abe Lincoln was born in Illinois.</p>
<p>
<label><input type="radio" name="question1" value="true" /> True </label>
<label><input type="radio" name="question1" value="false" /> False </label>
</p>
FIRST;
if (isset($_POST['submit'])) {
$selected_radio = $_POST['question1'];
if ($selected_radio == 'false') {
$correct++;
$_SESSION["correct"] = $correct;
print $correct;
}
}
}
if ($number == 1){
print <<<SECOND
<p>2. In what state was the battle of Gettysburg fought?</p>
<p>
<label><input type="checkbox" name="question2" value="Texas" /> a) Texas </label><br/>
<label><input type="checkbox" name="question2" value="Pennsylvania" /> b) Pennsylvania
</label><br/>
<label><input type="checkbox" name="question2" value="Virginia" /> c) Virginia </label><br/>
<label><input type="checkbox" name="question2" value="West Virginia" /> d) West Virginia
</label>
</p>
SECOND;
if (isset($_POST['submit'])) {
$selected_checkbox = $_POST['question2'];
if ($selected_checkbox == 'Pennslyvania') {
$correct++;
$_SESSION["correct"] = $correct;
print $correct;
}
}
}
if ($number == 2){
print <<<THIRD
<p>3. The last name of the commander of the Army of North Virginia was __________.</p>
<p>
<input type='text' id='question3' />
THIRD;
if (isset($_POST['submit'])) {
$selected_answer = $_POST['question3'];
if ($selected_answer == "lee") {
$correct++;
$_SESSION["correct"] = $correct;
}
}
}
if ($number >= $total_number)
{
print <<<FINAL_SCORE
Your final score is $correct correct out of $total_number. <br /><br />
Thank you for playing. <br /><br />
FINAL_SCORE;
session_destroy();
}
else
{
$number++;
$_SESSION["number"] = $number;
$script = $_SERVER['PHP_SELF'];
print <<<FORM
<form method = "post" action = $script>
<input type = "submit" value = "Check Answer" />
</form>
FORM;
}
print <<<BOTTOM
</body>
</html>
BOTTOM;
?>