Post Reply 
 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Easy Captcha in PHP
05-24-2010, 11:40 PM
Post: #1
Easy Captcha in PHP
This is simple Captcha script. You have comments inside the code and that enables you to manipulate with different Captcha data. You can change letters, digits, size of image, background color etc.
Here is the code :
PHP Code:
<?php
session_start
();
$mix "ABCDEFGHJKLMNPQRSTUVWXYZ23456789"// Characters to use in the captcha I have taken out 0,O,1,l,I to avoid confusion
$rand substr(str_shuffle($mix), 05); // Generate a random 5 character string from the $mix
$_SESSION['captcha_image_value'] = md5($rand); // Assign the randomly generated string to a session variable so we may reference it later

$image imagecreate(6030); // Create a 60 x 30 image
$bgColor imagecolorallocate ($image414141); // Assign a background color using the RGB values 41, 41 and 41
$textColorV imagecolorallocate ($image175175175);
$lineColorV imagecolorallocate ($image24000);
imageline($image,0,12,60,20,$lineColorV); // Draw a line from (0,12) to (60,20) of color $line color
imageline($image,0,20,60,12,$lineColorV);
imagestring ($image558$rand$textColorV); // Write the string to the image

// Some headers to prevent the image from being cached, thus ensuring the browser gets a new image every time the page is loaded
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0"false);
header("Pragma: no-cache");
header('Content-type: image/png'); // Makes browser recognize this script as an image

imagepng($image);
imagedestroy($image);
?>

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
05-25-2010, 03:07 AM (This post was last modified: 05-25-2010 03:09 AM by ajcis55.)
Post: #2
RE: Easy Captcha in PHP
Easy enough. For those confused about how to verify the user entered the correct captcha, you'll want to do something like this in your form submission:
Code:
<?php
session_start();
if(md5(txtCaptcha.value) ==  $_SESSION['captcha_image_value'])
    echo "Correct captcha";
else
    echo "Invalid captcha.";
?>

Free image hosting at http://myph.us.
Find all posts by this user
Quote this message in a reply
05-25-2010, 03:45 AM
Post: #3
RE: Easy Captcha in PHP
Yeah very good. This is because in the code we have :
Code:
$_SESSION['captcha_image_value'] = md5($rand);
That is using of md5 function on rand.
THX for reply !

There's a fine line between genius and insanity. I have erased this line.
Oscar Levant
There's a fine line between an administrator and black hat hacker. I have erased this line.
Dr DEBCOL
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Forum Jump:


 Quick Theme: