Miscellaneous

  Home arrow Miscellaneous arrow Page 5 - Creating a CAPTCHA with PHP
MISCELLANEOUS

Creating a CAPTCHA with PHP
By: Andrew Walsh
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 141
    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

  •  
     

    SEARCH CODEWALKERS

    TOOLS YOU CAN USE

    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

    blog comments powered by Disqus

    MISCELLANEOUS ARTICLES

    - Oracle Database XE: Indexes and Sequences
    - Modifying Tables in Oracle Database XE
    - Oracle Database XE: Tables and Constraints
    - More on Oracle Databases and Datatypes
    - Oracle Database XE Datatypes: Datetime and L...
    - Oracle Database XE Datatypes: Character and ...
    - From Databases to Datatypes
    - Firefox 3.6.6 Released with Improved Plug-in...
    - Attention Bloggers: WordPress 3.0 Now Releas...
    - Reflection in PHP 5
    - Inheritance and Other Advanced OOP Features
    - Advanced OOP Features
    - Linux from Scratch V.6.6 Review
    - Linux Gaining in Strength
    - Install Slackware on Your Old PC


    © 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap