Administer the IQ test
Everything's in line for you to take the IQ test. Now you initialize the session for the IQ test and iterate through five questions to determine a visiting user's fictitious IQ score. Each answer is saved as a session variable and retrieved in the next section for computing the final IQ score.
Processing "take the test" requests
When a user clicks the link shown in Figure 4 to begin the IQ test, you need to be able to capture that with your script and modify what your PHP displays back to that user accordingly. The code shown in Listing 7 does just that.
The first line checks to make sure that the link has been clicked by ensuring that the test variable in the URL or GET array is true. If the test hasn't begun yet, then the session variables get initialized with a call to the initTest() function -- which you define next in Initialize the session variables -- and a session variable, indicating that the test has begun, gets set to true. Also, if a visitor ever returns to the main start page where the Take the IQ test here text gets displayed, all the session variables get destroyed with the call to $session->destroy().
Listing 7. Starting the test
<?php
...
$session = java_session();
include('includes/functions.php');
if($_GET['test'] != '' && $_GET['test'] === "true") {
if($session->get(TEST_BEGUN) == null) {
include('header.php');
$session->put(TEST_BEGUN, new java("java.lang.Boolean", true));
initTest($session);
}
else{
...
}
}
else{
$session->destroy();
include('header.php');
print "Take your IQ test <a href='index.php?test=true'>".
"here</a><br>";
}
include('footer.php');
?>
|
Initialize the session variables
Here you initialize the session variables and display the first question back to the user. This enables your code to track which questions visitors have answered and which ones they haven't answered. Define the initTest() function in the functions.php file, as shown in Listing 8.
Listing 8. Initializing the session variables
function initTest($session){
$session->put(QUESTION_ONE, new java("java.lang.Boolean", false));
$session->put(QUESTION_TWO, new java("java.lang.Boolean", false));
$session->put(QUESTION_THREE, new java("java.lang.Boolean",
false));
$session->put(QUESTION_FOUR, new java("java.lang.Boolean", false));
$session->put(QUESTION_FIVE, new java("java.lang.Boolean", false));
questionOne($session);
}
|
The session variable for each question, which indicates whether the question has been answered or not, is set to false. When a question gets answered, you set the session variable to true (later in the tutorial).
Now you write the code to check whether or not a visitor has answered the first question, and if not, display it again. Continue defining the index.php file, as shown in Listing 9.
Listing 9. Checking to see if the first question has been answered
...
$session->put(TEST_BEGUN, new java("java.lang.Boolean", true));
initTest($session);
}
else{
do {
if($session->get(QUESTION_ONE)->booleanValue() === false) {
include('header.php');
questionOne($session);
break;
}
} while(false);
}
}
else{
include('header.php');
$session->destroy();
...
|
If the first question wasn't immediately answered, then it displays again until it is answered. Next you see the form and question used for the first question in the IQ test.
Here you use an HTML form to obtain feedback from the user on whether or not they know the answer to the given question. Define the questionOne() function, as shown in Listing 10.
Listing 10. Displaying the first question
function questionOne($session){
print "<h3>Question One</h3>";
print "<form action='index.php?test=true' method='POST'>".
"Are trees biotic or abiotic? ".
"<input name='".QUESTION_ONE_ANSWER."' /><br />".
"<input type='submit' value='Submit' /></form>";
}
|
Note that the form keeps passing along the test=true variable in the URL. Transmission is via POST; answers to form questions don't belong in the URL, because they can often be sensitive information. On top of that, variables passed via GET are saved in the URL when a page is bookmarked, and you wouldn't want someone to return to this page and unexpectedly answer a question they didn't intend to. See the browser output for question one in Figure 5.
Figure 5. Displaying question one
Next you process the answer submitted via this form.
Process the answer to question one
When a visitor submits an answer, you need to look for it in the POST array. Continue defining the index.php file, as shown in Listing 11.
Listing 11. Processing the answer to question one
...
include('includes/functions.php');
if($_POST[QUESTION_ONE_ANSWER] != ''){
$ans = $_POST[QUESTION_ONE_ANSWER];
$session->put(QUESTION_ONE, new java("java.lang.Boolean", true));
$session->put(QUESTION_ONE_ANSWER,
new java("java.lang.String", $ans));
}
if($_GET['test'] != '' && $_GET['test'] === "true") {
if($session->get(TEST_BEGUN) == null) {
...
|
Listing 11 checks to see if the POST array contains the answer to question one. If it does, it saves the answer as a session variable and modifies the session variable indicating whether or not question one has been answered from false to true. The next few panels go over displaying and processing the rest of the IQ test questions.
Similar to question one, if question one is answered and question two is unanswered, then question two needs to be displayed. Do so by continuing to define the index.php file, as shown in Listing 12.
Listing 12. Displaying question two
...
do {
if($session->get(QUESTION_ONE)->booleanValue() === false) {
include('header.php');
questionOne($session);
break;
} else
if($session->get(QUESTION_TWO)->booleanValue() === false) {
include('header.php');
questionTwo($session);
break;
}
} while(false);
...
|
The code in the if statement in bold executes when question one has been answered and question two has not. Also, it displays question two back to the visiting user by calling the questionTwo() function. Again, this is similar to question one. Define the questionTwo() function, as shown in Listing 13.
Listing 13. Displaying question two's form
function questionTwo($session){
print "<h3>Question two</h3>";
print "<form action='index.php?test=true' method='POST'>".
"Are you biotic or abiotic? ".
"<input name='".QUESTION_TWO_ANSWER."' /><br />".
"<input type='submit' value='Submit' /></form>";
}
|
Very similar to question one with a different question, the form is shown to the user, and submitting an answer brings you to the next panel.
This section processes the answers submitted from the forms for questions two through five. Continue defining the index.php file, as shown in Listing 14.
Listing 14. Processing answers for questions two through five
...
$session->put(QUESTION_ONE, new java("java.lang.Boolean", true));
$session->put(QUESTION_ONE_ANSWER,
new java("java.lang.String", $ans));
}
else if($_POST[QUESTION_TWO_ANSWER] != ''){
$ans = $_POST[QUESTION_TWO_ANSWER];
$session->put(QUESTION_TWO, new java("java.lang.Boolean", true));
$session->put(QUESTION_TWO_ANSWER,
new java("java.lang.String", $ans));
}
else if($_POST[QUESTION_THREE_ANSWER] != ''){
$ans = $_POST[QUESTION_THREE_ANSWER];
$session->put(QUESTION_THREE, new java("java.lang.Boolean", true));
$session->put(QUESTION_THREE_ANSWER,
new java("java.lang.String", $ans));
}
else if($_POST[QUESTION_FOUR_ANSWER] != ''){
$ans = $_POST[QUESTION_FOUR_ANSWER];
$session->put(QUESTION_FOUR, new java("java.lang.Boolean", true));
$session->put(QUESTION_FOUR_ANSWER,
new java("java.lang.String", $ans));
}
else if($_POST[QUESTION_FIVE_ANSWER] != ''){
$ans = $_POST[QUESTION_FIVE_ANSWER];
$session->put(QUESTION_FIVE, new java("java.lang.Boolean", true));
$session->put(QUESTION_FIVE_ANSWER,
new java("java.lang.String", $ans));
}
if($_GET['test'] != '' && $_GET['test'] === "true") {
if($session->get(TEST_BEGUN) == null) {
|
As each question gets answered, the session variable indicating whether it has been answered or not gets set from false to true, and the answer to the question is stored as a separate session variable.
Similar to questions one and two, unanswered questions need to be answered in succession. If the session variable indicates that the question remains unanswered, then the user is prompted to answer the given IQ test question. Continue to define the index.php file, as shown in Listing 15.
Listing 15. Displaying the rest of the unanswered questions
...
questionTwo($session);
break;
} else
if($session->get(QUESTION_THREE)->booleanValue() === false){
include('header.php');
questionThree($session);
break;
} else
if($session->get(QUESTION_FOUR)->booleanValue() === false){
include('header.php');
questionFour($session);
break;
} else
if($session->get(QUESTION_FIVE)->booleanValue() === false){
include('header.php');
questionFive($session);
break;
}
...
} while(false);
...
|
With code in place to display the questions, compete the functions.php file by defining the following three functions: questionThree(), questionFour(), and questionFive(), as shown in Listing 16.
Listing 16. Displaying questions three through five
function questionThree($session){
print "<h3>Question three</h3>";
print "<form action='index.php?test=true' method='POST'>".
"How many sides are in a square? ".
"<input name='".QUESTION_THREE_ANSWER."' /><br />".
"<input type='submit' value='Submit' /></form>";
}
function questionFour($session){
print "<h3>Question four</h3>";
print "<form action='index.php?test=true' method='POST'>".
"How many sides are in a circle? ".
"<input name='".QUESTION_FOUR_ANSWER."' /><br />".
"<input type='submit' value='Submit' /></form>";
}
function questionFive($session){
print "<h3>Question Five</h3>";
print "<form action='index.php?test=true' method='POST'>".
"What is 1 plus 1? ".
"<input name='".QUESTION_FIVE_ANSWER."' /><br />".
"<input type='submit' value='Submit' /></form>";
}
|
Each question is now displayed appropriately. Now, on to shared PHP/JSP sessions.




