An Object-Based Approach to Building Noisy Images - Creating noisy images with a procedural approach
(Page 2 of 4 )
Before I proceed to demonstrate how to create noisy images by using an object-oriented approach, first let me show you the procedural version of this application. It will help you spot more clearly the differences between both techniques, and eventually choose the one that best suits your personal needs.
Okay, having said that, here is the definition of the PHP file created in the previous article. As you'll remember, it is responsible for generating four-digit random values, as well as for displaying a simple contact form:
<?php
// define 'getChallengeValue()' function
function getChallengeValue($length=4){
$chars="ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
if(!is_int($length)||$length<1||$length>strlen($chars)){
trigger_error('Invalid length for random
string',E_USER_ERROR);
}
$rndstr='';
$maxvalue=strlen($chars)-1;
for($i=0;$i<$length;$i++){
$rndstr.=substr($chars,rand(0,$maxvalue),1);
}
return $rndstr;
}
session_start();
// save challenge value to session variable
$_SESSION['challenge']=getChallengeValue();
?>
<!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" />
<title>Example using simple noisy image</title>
<style type="text/css">
body{
padding: 0;
margin: 0;
background: #fff;
}
h1{
font: bold 16px Arial, Helvetica, sans-serif;
color: #000;
text-align: center;
}
textarea{
width: 250px;
padding: 2px 0 2px 0;
font: normal 11px Verdana, Arial, Helvetica, sans-serif;
color: #000;
border: 1px solid #999;
}
#formcontainer{
width: 35%;
padding: 10px 100px 10px 10px;
margin-left: auto;
margin-right: auto;
background: #eee;
border: 1px solid #999;
}
#formcontainer p{
font: bold 11px Tahoma, Arial, Helvetica, sans-serif;
text-align: right;
}
.challengevalue{
display: block;
float: right;
width: 230px;
padding: 7px 9px 7px 10px;
margin: 10px 0 20px 0;
background: #fff url(noisy_image.gif) left center repeat-x;
font: bold 20px Arial, Helvetica, sans-serif;
color: #039;
text-align: center;
border: 1px solid #999;
}
.inputbox{
width: 250px;
padding: 3px 0 3px 0;
font: normal 11px Verdana, Arial, Helvetica, sans-serif;
color: #000;
border: 1px solid #999;
}
.formbutton{
width: 80px;
padding: 3px 0 3px 0;
font: bold 11px Verdana, Arial, Helvetica, sans-serif;
color: #000;
}
</style>
</head>
<body>
<h1>Example using simple noisy image</h1>
<div id="formcontainer">
<form action="check_data.php" method="post">
<p>First Name <input type="text" name="fname" title="Enter
your first name" class="inputbox" /></p>
<p>Last Name <input type="text" name="lname" title="Enter
your last name" class="inputbox" /></p>
<p>Email <input type="text" name="email" title="Enter your
email address" class="inputbox" /></p>
<p>Enter the four-digit code shown below:</p>
<p><span class="challengevalue"><?php echo $_SESSION
['challenge'];?></span></p>
<p><input type="text" name="challenge" title="Enter the
code shown above" class="inputbox" /></p>
<p>Enter the text of your message:</p>
<p><textarea name="message" title="Enter the text of your
message here" rows="10" cols="10"></textarea></p>
<p><input type="submit" name="send" value="Send Data"
class="formbutton" /></p>
</form>
</div>
</body>
</html>
Believe or not, the above PHP file is all the source code that you need to implement a basic noisy image in any online form, including the one shown previously. However, as you may have noticed, the file in question first uses a PHP function to generate the random value, and then saves this string to a session variable.
Logically, the purpose of doing this is merely to maintain the value of the aforementioned random string across different web pages. This allows for the verification of this value on the web server after the online form has been submitted. As you can see, this process is mostly ruled by common sense rather than a complex technique, and it shouldn't be hard to understand at all.
All right, at this stage I believe that building noisy images by using a procedural approach is already a familiar concept to you. Therefore, the next step consists of replacing the "getChallenge()" PHP function with a simple class that performs the same task, but obviously using the object-oriented paradigm.
Do you want to see how this brand new PHP class will be built? Click on the link that appears below and keep reading.
Next: Building a noisy image with an object-based approach >>
More DHTML Articles
More By Alejandro Gervasio