Designing PHP-Based Solutions: Best Practice example from a Well House Consultants training course
More on Designing PHP-Based Solutions: Best Practice
[link]
This example is described in the following article(s): • Check your user is human. Have him retype a word in a graphic - [link] |
If you're searching for a page where you can try this code,
select here |
Source code: gruse.php Module: H115
<?php
/* Provides a login capability where the web site visitor
is asked to copy back a word or string of characters that
are displayed in a graphic. This capability is known as
Captcha. */
/*
Page 1 - offer login
Page 2 - offer application form
Page 3 - logout
Files required ...
a) This file
b) grgen.php (generate the graphics)
c) appropriate TTF font files
*/
session_name("grd"); // keep apart from other session demos
session_start();
$current = $_SESSION[current];
if (! $current) $current = 0;
$fill = array();
$fill[errmsg] = "";
$fill[result] = "";
// Finish up from previous page
///////////////////////////////
switch ($current) {
case 0: # Initialise
$current = 1;
break;
case 1: # Check Input
$cooked = strtoupper($_REQUEST[youtyped]);
$cooked = preg_replace('/1/','I',$cooked);
$cooked = preg_replace('/0/','O',$cooked);
if ($cooked == $_SESSION[word]) {
$current = 2;
} else {
$fill["errmsg"] = "That was NOT correct<br>";
}
break;
case 2: # Live application
if ($_REQUEST[action] == "logout") {
$current = 3;
} else {
$fill[result] = "You entered <b>".
htmlspecialchars(stripslashes($_REQUEST[said])).
"</b><br><br>";
}
break;
}
$_SESSION[current] = $current;
$fill["current"] = $current;
$fill["of"] = 3;
// Prepare for next page
////////////////////////
switch ($current) {
case 1: # Prepare Graphic
$length = rand(3,7);
$string = "";
for ($k=0; $k<$length; $k++) {
$char = rand(1,26);
$string .= substr("QWERTYUIOPASDFGHJKLZXCVBNM",$char-1,1);
}
$_SESSION["word"] = $string;
$fill["instruct"] = "<img src=grgen.php>";
$fill["form"] = "<form method=post>";
$fill["form"] .= "<br>Please enter the word you see";
$fill["form"] .= "<input name=youtyped size=8><br>";
$fill["form"] .= "<input name=action type=submit>";
$fill["form"] .= "</form>";
break;
case 2: # Offer live application
$fill["instruct"] = "<font color=brown>You are logged in</font>";
$fill["form"] = "<form method=post>";
$fill["form"] .= "<br>Please enter a piece of text";
$fill["form"] .= "<input name=said><br>";
$fill["form"] .= "<input name=action type=submit>";
$fill["form"] .= " or <input name=action type=submit value=logout>";
$fill["form"] .= "</form>";
break;
case 3: # Log out
session_destroy();
$fill["instruct"] = "<font color=brown>You have logged out</font>";
$fill["form"] = "<br><br>Link - <a href=$PHP_SELF>Restart demo</a><br>";
$fill["form"] .= "Link - <a href=/>Home Page</a><br>";
break;
}
?>
<html>
<head>
<title>Demonstration of Capthca driver in PHP</title>
</head>
<body>
<h1>Logging in by typing text in a graphic</h1>
This is a demonstration (with full source code available) showing how you
can write / run a PHP application to get your users to validate their
presence by re-typing the text in a graphic. Such techniques are know as
<b>Captcha</b> (Completely Automated Public Turing Test to Tell
Computers and Humans Apart).<br>
<h2>Page <?= $fill[current] ?> of <?= $fill[of] ?></h2><br>
<?= $fill[result] ?>
<?= $fill[errmsg] ?>
<?= $fill[instruct] ?>
<?= $fill[form] ?>
<br><br><i>This demonstration provides a human-identifying login capability
via a graphic. Users are required to copy a string on 3 to 7 letters that
are displayed. Although the strings offered are letters only and all upper
case, the code accepts lower case inputs, and also 0 for O and 1 for I.
<br><br>Once logged in, all you'll see will be an echo application -
this is a demonstration of the 'real' user identification technology.</i>
<hr>
Demonstration by <a href=http://www.grahamellis.co.uk>Graham Ellis</a> of
<a href=http://www.wellho.net>Well House Consultants</a><br><br>
Source code -
<a href=http://www.wellho.net/resources/ex.php4?item=h115/gruse.php>this page</a>
and
<a href=http://www.wellho.net/resources/ex.php4?item=h115/grgen.php>graphic
generator script</a> <br><br>
If you use the source code of this demonstration on your site, please
provide a link back to us. See
<a href=http://www.wellho.net/resources/linktous.html>here</a>
</body>
</html>
Learn about this subject
Books covering this topic
Yes. We have over 700 books in our library.
Books
covering PHP are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "Designing PHP-Based Solutions: Best Practice" training module. You'll find a description of the topic and some
other closely related examples on the
"Designing PHP-Based Solutions: Best Practice" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from
our download centre under an
Open Training Notes License.
Other resources
• Our
Solutions centre provides a number of longer technical articles.
• Our
Opentalk forum archive provides a question and answer centre.
•
The Horse's mouth provides a daily tip or thought.
• Further resources are available via the
resources centre.
• All of these resources can be searched through through our
search engine
• And there's a global index
here.
Web site author
This web site is written and maintained by
Well House Consultants.
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job.
Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are
forbidden from using it for running your own training courses
without our prior written permission. See
our
page on courseware provision for more details.
Any of our images within this code may NOT be reused on a public URL without our
prior permission. For Bona Fide personal use, we will often grant you permission provided
that you provide a link back. Commercial use on a website will incur a license fee for
each image used - details on request.