PHP
  Home arrow PHP arrow Page 2 - Designing a Captcha System with PHP and MySQL
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
PHP

Designing a Captcha System with PHP and MySQL
By: Codex-M
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 8
    2009-06-04


    Table of Contents:
  • Designing a Captcha System with PHP and MySQL
  • The Captcha Image Generation Script
  • The PHP Form with Captcha-Generated Challenge
  • Captcha System Without GD Support

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    Designing a Captcha System with PHP and MySQL - The Captcha Image Generation Script
    ( Page 2 of 4 )

    For simplicity of this illustration, we will use random numbers and convert them to images to be used as captcha. Then we will use session and other GD components to generate images. Below is the PHP script, which we call captxt.php

    <?php

    //start session which will be used to store generated numbers of validation in the form

    session_start();

    //generate random number between 10,000 and 99999

    $number =mt_rand(10000, 99999);

    //store generate random number to a session

    $_SESSION['answer']=$number;

    //create image 50 x 50 pixels

    $imagecreate = imagecreate(50, 50);

    // white background and blue text

    $background = imagecolorallocate($imagecreate, 255, 255, 255);

    $textcolor = imagecolorallocate($imagecreate, 0, 0, 255);

    // write the string at the top left

    imagestring($imagecreate, 5, 5, 10, $number, $textcolor);

    // output the image

    header("Content-type: image/png");

    $image= imagepng($imagecreate);

    ?>

    Let's discuss the process for you to improve this design:

    1. session_start(); is required in the first line of every PHP script if we want to store variables in the session. Storing variables in the session makes it available for use in other files that also use the session. This will be used to test whether the generated random number matches the one typed in by the user.

    2. $number =mt_rand(10000, 99999); will generate random numbers in the range of 10,000 to 99,999 and store them the $number variable.

    3. $_SESSION['answer']=$number; will store the generated random number to a session array so that it can be used in the PHP form script that will test if the user's answer matches the generated code.

    4. And finally this piece code:

    //create image 50 x 50 pixels

    $imagecreate = imagecreate(50, 50);

    // white background and blue text

    $background = imagecolorallocate($imagecreate, 255, 255, 255);

    $textcolor = imagecolorallocate($imagecreate, 0, 0, 255);

    // write the string at the top left

    imagestring($imagecreate, 5, 5, 10, $number, $textcolor);

    // output the image

    header("Content-type: image/png");

    $image= imagepng($imagecreate);

    The function creates a space 50 x 50 pixel square, using a white background and blue text. This is purely customizable; for the sake of simplicity we do not include complex backgrounds, as you have seen in some captcha systems.



     
     
    >>> More PHP Articles          >>> More By Codex-M
     

       

    PHP ARTICLES

    - Adding Ordering and Grouping Clauses to the ...
    - Implementing Factory Methods in PHP 5
    - Merging a File Split for FTP Upload using PHP
    - Getting Data from Yahoo Site Explorer Inboun...
    - Method Chaining: Adding More Selecting Metho...
    - How to Split a File During an FTP Upload Usi...
    - Expanding a Custom CodeIgniter Library with ...
    - Using the Yahoo Site Explorer Inbound Links ...
    - Building a CodeIgniter Custom Library with M...
    - Building an E-mini Trading System Using PHP ...
    - Completing the MySQL Class with Method Chain...
    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek