Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
CAPTCHA !! " C ompletely  A utomated  P ublic  T uring test to tell  C omputers and  H umans  A part.
This is How They Look
Characteristics <ul><li>Provide a problem easy enough for all humans to solve.  </li></ul><ul><li>Prevents standard automa...
Applications <ul><li>protect systems vulnerable to e-mail spam, such as the webmail services of Gmail, Hotmail, and Yahoo!...
Creating a CAPTCHA with PHP  <ul><li>Random text generated  </li></ul><ul><li>Text written to image </li></ul><ul><li>Text...
Creating Random Text <ul><li>microtime() and mktime() - to generate a number.  </li></ul><ul><li>This number will then be ...
Writing Text to the Image <ul><li>Loading the basic image from CAPTCHA.png  </li></ul><ul><li>allocate the color to a vari...
Check if User entered code Correctly <ul><li>simple text form that requires a code to be entered  </li></ul><ul><li>you ju...
Improvements <ul><li>Use a TTF font  </li></ul><ul><li>Move the lines randomly  </li></ul><ul><li>Randomly position the te...
Source Code -Creating Random Text <ul><li><?php //Start the session so we can store what the code actually is .  session_s...
Writing Text to the Image <ul><li>/*  Now for the GD stuff, for ease of use lets create the image from a background image....
Writing Text to the Image <ul><li><?php /*  Now for the all important writing of the randomly generated string to the imag...
Check if User entered code Correctly <ul><li><?php session_start();  //Encrypt the posted code field and then compare with...
<ul><li>THANK YOU !!! </li></ul>
Upcoming SlideShare
Loading in …5
×

Captcha Creation Using PHP

3,013 views

Published on

Creating Simple CAPTCHA Using PHP

  • Be the first to comment

Captcha Creation Using PHP

  1. 1. CAPTCHA !! &quot; C ompletely A utomated P ublic T uring test to tell C omputers and H umans A part.
  2. 2. This is How They Look
  3. 3. Characteristics <ul><li>Provide a problem easy enough for all humans to solve. </li></ul><ul><li>Prevents standard automated software from filling out a form, without being specific designed to circumvent specific CAPTCHA systems. </li></ul>
  4. 4. Applications <ul><li>protect systems vulnerable to e-mail spam, such as the webmail services of Gmail, Hotmail, and Yahoo! Mail. </li></ul><ul><li>active use in stopping automated posting to blogs, forums and wikis </li></ul><ul><li>CAPTCHAs display distorted text that is difficult to read by character recognition software </li></ul>
  5. 5. Creating a CAPTCHA with PHP <ul><li>Random text generated </li></ul><ul><li>Text written to image </li></ul><ul><li>Text stored in session/cookie/database </li></ul><ul><li>Image displayed to user </li></ul><ul><li>User enters the code </li></ul><ul><li>User entered code is checked against the stored key </li></ul><ul><li>If they match then something is done </li></ul>
  6. 6. Creating Random Text <ul><li>microtime() and mktime() - to generate a number. </li></ul><ul><li>This number will then be encrypted using md5(). </li></ul><ul><li>With this 32 character long encrypted string we will then use substr() to cut it down to a 5 letter long string. This is our random text. </li></ul>
  7. 7. Writing Text to the Image <ul><li>Loading the basic image from CAPTCHA.png </li></ul><ul><li>allocate the color to a variable,with imagecolorallocate() </li></ul><ul><li>write the text to the image which is made easy with imagestring() </li></ul><ul><li>output-done by setting the content type of the page to image/png with header() and outputting the image to the browser with imagepng() </li></ul><ul><li>Note: string is encrypted and stored in the session variable $_SESSION['key'] </li></ul>
  8. 8. Check if User entered code Correctly <ul><li>simple text form that requires a code to be entered </li></ul><ul><li>you just display the image to the user with a simple <img xsrc=&quot;captcha.php&quot; border=&quot;0&quot;> tag </li></ul><ul><li>Perform checking with the input from user </li></ul>
  9. 9. Improvements <ul><li>Use a TTF font </li></ul><ul><li>Move the lines randomly </li></ul><ul><li>Randomly position the text on the image </li></ul><ul><li>Rotate the text randomly </li></ul><ul><li>Use words instead of that string (ie, have a randomly picked word out of say a file of about 1000) </li></ul>
  10. 10. Source Code -Creating Random Text <ul><li><?php //Start the session so we can store what the code actually is . session_start(); //Now lets use md5 to generate a totally random string $md5 = md5(microtime() * mktime()); /* We dont need a 32 character long string so we trim it down to 5 */ $string = substr($md5,0,5); ?> </li></ul>
  11. 11. Writing Text to the Image <ul><li>/* Now for the GD stuff, for ease of use lets create the image from a background image. */ $captcha = imagecreatefrompng(&quot;./captcha.png&quot;); /* Lets set the colours, the colour $line is used to generate lines.  Using a blue misty colours. The colour codes are in RGB */ $black = imagecolorallocate($captcha, 0, 0, 0); $line = imagecolorallocate($captcha,233,239,239); imageline($captcha,0,0,39,29,$line); imageline($captcha,40,0,64,29,$line); ?> </li></ul>
  12. 12. Writing Text to the Image <ul><li><?php /* Now for the all important writing of the randomly generated string to the image. */ imagestring($captcha, 5, 20, 10, $string, $black); /* Encrypt and store the key inside of a session */ $_SESSION['key'] = md5($string); /* Output the image */ header(&quot;Content-type: image/png&quot;); imagepng($captcha); ?> </li></ul>
  13. 13. Check if User entered code Correctly <ul><li><?php session_start(); //Encrypt the posted code field and then compare with stored key if(md5($_POST['code']) != $_SESSION['key']) {   die(&quot;Error: You must enter the code correctly&quot;); } </li></ul><ul><li>Else </li></ul><ul><li>{   echo 'You entered the code correctly'; } </li></ul><ul><li>?> </li></ul>
  14. 14. <ul><li>THANK YOU !!! </li></ul>

×