Home arrow PHP arrow Page 4 - Security Images in PHP

Code for 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 complete code listing for securityimage.php should now look like this:

<?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());
}

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

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

//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)];
}

//Create random size, angle, and dark color
$size = rand(12, 16);
$angle = rand(-5, 5);
$color = ImageColorAllocate($im, rand(0, 100), rand(0, 100), rand(0, 100));

//Determine text size, and use dimensions to generate x & y coordinates
$textsize = imagettfbbox($size, $angle, $font, $textstr);
$twidth = abs($textsize[2]-$textsize[0]);
$theight = abs($textsize[5]-$textsize[3]);
$x = (imagesx($im)/2)-($twidth/2)+(rand(-20, 20));
$y = (imagesy($im))-($theight/2);

//Add text to image
ImageTTFText($im, $size, $angle, $x, $y, $color, $font, $textstr);

//Output PNG Image
header("Content-Type: image/png");
ImagePNG($im);

//Destroy the image to free memory
imagedestroy($im);

//Insert reference into database, and delete any old ones
mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db("dw_php");
//Create reference
mysql_query("INSERT INTO security_images (insertdate, referenceid, hiddentext) VALUES (
now(), '".$referenceid."', '".$textstr."')");
//Delete references older than 1 day
mysql_query("DELETE FROM security_images
WHERE insertdate < date_sub(now(), interval 1 day)");

//End Output
exit;

?>

Open securityimage.php in a browser. You should see something like the image below:

Security Images in PHP

Now, we are finished with the file and ready to put it to use!

Note: If you receive a message about ImageCreateFromPNG being an undefined function, this is probably because you don't have the GD Graphics Library installed. Install this (download may be obtained from http://www.boutell.com/gd/) and retry.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- 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...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: