Miscellaneous
  Home arrow Miscellaneous arrow Page 5 - Creating a CAPTCHA with PHP
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS

Creating a CAPTCHA with PHP
By: Andrew Walsh
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 78
    2006-06-06

    Table of Contents:
  • Creating a CAPTCHA with PHP
  • How does a CAPTCHA work?
  • Creating the random text
  • Writing the text to the image
  • Check if the user entered the code correctly
  • Improvements and Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Creating a CAPTCHA with PHP - Check if the user entered the code correctly


    (Page 5 of 6 )

    To check if the user entered the code correctly you must first allow the user to do this. You can do this with a simple text form that requires a code to be entered, a simple text field called code or something similar should do nicely. Then you just display the image to the user with a simple <img xsrc="captcha.php" border="0"> tag. It is really too low a level to show you how to make a form like this, if you don't know how to make a form like I described above then this tutorial is probably not for you.

    Now assuming that this form has been submitted we need to check if the code matches what was on the image, after all this is the whole point of a captcha system. You can do this in any php file as long as the form described above submits to it. For basic checking we will use the code below.

    <?php
    session_start
    ();

    //Encrypt the posted code field and then compare with the stored key

    if(md5($_POST['code']) != $_SESSION['key'])
    {
      die(
    "Error: You must enter the code correctly");
    }else{
      echo 
    'You entered the code correctly';
    }
    ?>

    The session_start() you see here simply continues the session from the previous page, easy enough. Then its just a case of simple text matching which you can see is done by the if statement.

    More Miscellaneous Articles
    More By Andrew Walsh


       · The Text: As you can see from the code above we are loading the basic image from...
       · Ok it will be corrected soon
       · My bad...I fixed this yesterday...
       · I have been meaning to get a Captcha image installed on my little application. As...
       · Can i use this code to play around and use it in a wordpress (http://wordpress.org)...
       · Problem to use in Post nukewhen i impliment the captcha on post nuke web then i...
       · You can do what you like with it, although i thought wordpress included a much...
       · By changing this line:$string = substr($md5,0,5);With the following gives you...
       · By changing this line:$string = substr($md5,0,5);With the following gives you...
       · I get a problem that dont show the picture it shows alot of strange text...
       · &lt;?php//Start the session so we can store what the code actually...
       · This was the base of the new captcha I made, except I did things a little...
       · please add this to the headers partheader(&quot;Cache-Control: no-cache,...
       · All goes well on a new page but I get this message when I incorporate it in an...
       · I get the same error when I don't start the session above the <html> tags,...
       · The site http://www.trycaptcha.com lets you try a bunch of open source CAPTCHA...
       · This tutorial really helped me out, thanks for the source!
       · Thanks for helpful little tutorial on how to do CAPTCHA! Never had to think about it...
       · The code would be great at prevent bots, but it has a critical flaw that would let...
       · please give me captcha work
       · You can this code to wordpresss also
       · I like the way you used the md5 for the random text. Much easier than how I was...
     

    MISCELLANEOUS ARTICLES

    - Using PHP to Stream MP3 Files and Prevent Il...
    - 10 Must Have Firefox Improvements
    - All About OpenOffice 3.0
    - Shell Script Writing
    - Loops in the UNIX Shell
    - The Test in the UNIX Shell
    - Data Streams and the UNIX Shell
    - Control Mechanisms of the UNIX Shell
    - Variables Within the UNIX Shell
    - The Shell and UNIX
    - In Detail: UNIX File Systems
    - Rights Management in UNIX
    - UNIX File Systems
    - The Terminal in UNIX
    - Operating Systems and UNIX





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT