Security Images in PHP - Getting Started -- Securityimage.php (
Page 2 of 6 )
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)];
}
| | 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! | | | | | |
|
 |