Home arrow PHP arrow Page 2 - Security Images in PHP

Getting Started -- Securityimage.php - PHP

Learn how to create a sign-up form for a website with a security image. The image prevents fake sign-ups and spam. In this tutorial, we will learn how to create a security image template, then put it to use.

TABLE OF CONTENTS:
  1. Security Images in PHP
  2. Getting Started -- Securityimage.php
  3. Set Other Text Variables
  4. Code for securityimage.php
  5. Creating the Sign-up Demo -- Signupdemo.php
  6. Form Handler Script
By: Nathan Rohler
Rating: starstarstarstarstar / 113
September 08, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

The first page to create is the page which will generate the security images, securityimage.php. First, we need to open the PHP block and determine if we can use refid in the querystring; otherwise, we generate a unique one ourselves.

<?php
//Generate Reference ID
if (isset($HTTP_GET_VARS["refid"]) && $HTTP_GET_VARS["refid"]!="") {
   $referenceid = stripslashes($HTTP_GET_VARS["refid"]);
} else {
   $referenceid = md5(mktime()*rand());
}

Now, we must select a font to use. For this demo, we will use Century, because it is easy to distinguish between zeroes and o's, and because it is a serif font, it is harder for a computer to comprehend. You will need to set the path to the font, reflecting what OS you are using and where the fonts folder is.

//Select Font
$font = "C:\WINDOWS\Fonts\Century.ttf";

Next, we will randomly select one of the three backgrounds (from the included files). Using one of these files, we initialize a new image identifier $im, representing the background image, using ImageCreateFromPNG. If you want more variety, you can create more backgrounds (must be PNG format).

//Select random background image
$bgurl = rand(1, 3);
$im = ImageCreateFromPNG("images/bg".$bgurl.".png");

After this, we will generate a random string. To do this, we first create an array of characters to use. Then, we specify a length for the text to be generated. For this demonstration, we will use 8, but you can change it to any length you wish. Finally, we create an empty variable, then randomly fill it with characters in a loop.

//Generate the random string
$chars = array("a","A","b","B","c","C","d","D","e","E","f","F","g","G",
"h","H","i","I","j","J","k",
"K","l","L","m","M","n","N","o","O","p","P","q","Q","r",
"R","s","S","t","T","u","U","v",
"V","w","W","x","X","y","Y","z","Z","1","2","3","4","5",
"6","7","8","9");
$length = 8;
$textstr = "";
for ($i=0; $i<$length; $i++) {
   $textstr .= $chars[rand(0, count($chars)-1)];
}



 
 
>>> More PHP Articles          >>> More By Nathan Rohler
 

blog comments powered by Disqus
   

PHP ARTICLES

- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 9 - Follow our Sitemap

Dev Shed Tutorial Topics: