You can download the entire source code for this series here PHP Quiz (10.88 kB)

If you take a look at ‘index.php’ from Part 3, you’ll see there are 2 forms, one for users who wish to register and one for those that don’t (‘Just Take The Test’). Both of these forms send the user to ‘test.php’ so ‘test.php’ needs to check which form the user submitted. We can achieve this by checking to see if the hidden form field called ‘register’ that is only in the ‘register’ form was submitted. It looks like this:

<input type="hidden" name="register" value="TRUE" />

We can check for this hidden field using the $_POST array. At the top of ‘test.php’, just after our ‘require_once’ lines, we place the following if/else conditional statement:

if (isset($_POST['register'])) {
User wants to register ....
} else {
just take the test ...
} //end of if/else conditional</strong>

Different code will be executed depending on the result of this check. For example, we can assign them a username:

Assign A Username

if (isset($_POST['register'])) { // they want to register
    $username = trim(strip_tags(stripslashes($_POST['username'])));
    if (ini_get('magic_quotes_gpc')) {
        $username = stripslashes($username);
    }
} else { //they don't want to register
    $random = rand(1,1000);
    $username = 'Anon'. $random;
} //end of if/else conditional</strong>
$num = 0;

If the user has chosen a username, we clean it up and assign it to the variable $username. To clean it up we use 2 functions, strip_tags() and trim().
strip_tags() removes any html tags. This stops malicious code/scripts from being submitted to the form.
trim() removes any leading or trailing whitespace from the form input. We also check to see if magic quotes are enabled. If they are we use stripslashes() to remove any escaping back slashes that our script would otherwise produce, for example on a username with an apostrophe such as O’ Toole.

If the user hasn’t supplied a username, we generate a random one for them. rand() returns a random number between the 2 given parameters, in this case between 1 and 1000. The random number is then appended onto the string ‘Anon’ and is then assigned to $username.

Regardless of the if/else conditional, we also initialise a variable called $num and set it to 0. We’ll use this variable as a counter to determine which set of question/answers to display on the page.

We also need a way to store $username so that we can access it on all pages. We do this by using sessions. A session is a way to store user information on the server and enables the server to recognise the user through multiple pages, by using a unique identifier for each user.

Use Sessions

if (isset($_POST['register'])) {
    $username = trim(strip_tags(stripslashes($_POST['username'])));
} else {
    $random = rand(1,1000);
    $username = 'Anon'. $random;
} //end of if/else conditional</strong>
$num = 0;
$_SESSION['user'] = $username;

Here we’ve created a session variable called ‘user’ which stores our $username variable. As well as the username, there are other pieces of information that would be handy stored in the $_SESSION variable.

Add More Session Variables

if (isset($_POST['register'])) {
    $username = trim(strip_tags(stripslashes($_POST['username'])));
} else {
    $random = rand(1,1000);
    $username = 'Anon'. $random;
} //end of if/else conditional</strong>
$num = 0;
$_SESSION['user'] = $username; // username
$_SESSION['score'] = 0; // score set to 0
$_SESSION['correct'] = array(); // to hold the user's correct answers
$_SESSION['wrong'] = array(); // to hold the user's incorrect answers
$_SESSION['finished'] = 'no'; // they haven't finished the quiz yet

So, we’ve checked to see if the user wants to register a name and we’ve set the appropriate session variables.
We can access these on the same page they were set. Just before the closing tag of the #intro div, place the following:

<?php if(isset($_SESSION['user'])) echo "<h4>Current tester:{$_SESSION['user']}</h4>"; ?>
</div><!--intro-->

Now, when you submit either of the forms on ‘index.php’, you should be taken to ‘test.php’ and either the submitted username or a randomly generated one will be printed out as a level 4 heading.

That’s about it for Part 4. In Part 5, we’ll look at creating an xml file to store our users and their scores in. We’ll use simpleXML to extract the data and display it on our pages and we’ll delve a bit more in to using sessions.

Before I go, here is the html, as promised to ‘mug’ after the last post, for ‘results.php’. There’s not a lot to it to be honest as most of the info is pulled out of the xml file. I’ve therefore created some dummy data so you can see how it would look.

results.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" href="style.css" type="text/css" />
<title>The Web Acronym Test Results</title>
</head>
<body id="resultpage">
<div id="wrapresult">
<h1>The Results Page For </h1>
<div id="intro">
<h2>Top 20 Scorers</h2>
<ul class="leaders">
    <li>Anon817: 20/20</li>
    <li>Anon860: 20/20</li>
    <li>Anon283: 20/20</li>
    <li>Scott S: 20/20</li>
    <li>Andy: 20/20</li>
    <li>bocaj: 20/20</li>
    <li>Anon261: 20/20</li>
    <li>ElanMan: 20/20</li>
</ul>
</div><!--intro-->
<div id="quiz">
<div class="qanda clear">
    <h4>Anacronym1: FTP</h4>
    <ol>
        <li class="correct">File Transfer Protocol</li>
        <li>Force Through Privately</li>
        <li class="wrong">File Through Protocol (Woops!)</li>
        <li>File Test Protocol</li>
    </ol>
</div>
<div class="qanda">
    <h4>Anacronym2: AJAX</h4>
    <ol>
        <li class="correctuser">Asynchronous JavaScript and XML (Correct!)</li>
        <li>All JavaScript and XML</li>
        <li>Alternative Java and XML</li>
        <li>Actual JavaScript and XML</li>
    </ol>
</div>
<div class="qanda clear">
    <h4>Anacronym3: RSS</h4>
    <ol>
        <li class="correct">Really Simple Syndication</li>
        <li>Really Simple Scripting</li>
        <li>Ready-Styled Scripting</li>
        <li class="wrong">Really Stupid Syndication (Woops!)</li>
    </ol>
</div>
<div class="qanda">
    <h4>Anacronym4: XSS</h4>
    <ol>
        <li class="correctuser">Cross-site Scripting (Correct!)</li>
        <li>Cross-site Security</li>
        <li>Cleverly Structured Scripting</li>
        <li>eXtremely Safe and Secure</li>
    </ol>
</div>
</div><!--quiz-->
<ul id="footer" class="clear">
    <li><a href="index.php" title="Start The Quiz Again">Start Again</a></li>
</ul>
</div><!--wrapper-->
</body>
</html>
Part 1
  • Part 2
  • Part 3
  • Part 5
  • Part 6
  • Final Part
  • Similar Posts:

       
    © 2012 ElanManSuffusion theme by Sayontan Sinha

    Page optimized by WP Minify WordPress Plugin