Security Images in PHP - Code for securityimage.php (
Page 4 of 6 )
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:

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.
| | Discuss Security Images in PHP | | | | | | | Good article, I would cut out the hidden form fields and database and use session... | | | | | | I think the author needs to upgrade their version of php, because $HTTP_GET_VARS is... | | | | | | maybe it would be better not to store $hiddentext in mysql, but its... | | | | | | Very good article
[url=http://www.virtual-interconnect.co.uk/]Visit us[/url] | | | | | | Seeing PHP3 code when PHP5 is current is a bit disappointing. The author should... | | | | | | ...need to check random word through a list of profane words so you don't get the... | | | | | | May not be supported, but it's still in use in a lot of places. | | | | | | Wouldn't it even be possible to store an md5-hash in the form itself? That way there... | | | | | | I get this error: "Fatal error: Call to undefined function: imagecreatefrompng() in... | | | | | | hi, i think changing the option from png to jpeg will work fine . though some of the... | | | | | | in function checkSecurityImage, after veirfying that row count is more then zero,... | | | | | | Only if the hash mix the string and some other text known only by the php... | | | | | | Great article! Helped me accomplish my task very quickly. Thanks for writing this... | | | | | | This is a great article! I had some issues though with the implementation of... | | | | | | I Think this article can be strengthened by the fuction imagecopyresized() where if... | | | | | | The problem with the database method used in this example is that a bot could fill... | | | | | | Only site visitors with cookies enabled would be able to use it. | | | | | | Nice article. I'm having one problem though, there's no text on the generated image.... | | | | | | You can always have a small font.ttf and host the file in the same folder. but try a... | | | | | | When you don't see the text, you have to change length in $length (for ($i=0;... | | | | | | Do the support files work? I noticed things like form tags missing etc...
When I... | | | | | | Or just remove the vowels to avoid spelling any english word | | | | | | It says that there are some files included, but they're not.. Handy handy | | | | | | Hi,
I am unable to get this to work - has anyone ahd any luck wih it. The data... | | | | | | I'm not a php programmer.
I'm carpenter.
This article helped me best to put... | | | | | | Since I'm a beginner to php, it's my first time come into GD library.
This article... | | | | | | This way gives you 24 hours to post, nice for people like me who leave the page open... | | | | | | I dont what happen but my text in my image security is not there..why?tell me? | | | | | | I am beginner PHP programmer. I like your article. From your article I got better... | | | | | | >>> Post your comment now! | | | | | |
|
 |