PHP
  Home arrow PHP arrow Page 4 - 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 - Captcha System Without GD Support
    ( Page 4 of 4 )

    Without GD support, things will be slightly more complicated but certainly not impossible. The following is a realistic strategy you can implement to create captcha images without GD support:

    1. Create captcha in your local computer using your favorite photo editor. You can even make it very challenging as long as it is still readable at your desired image size.

    2. Upload all the images to your FTP server specifying a clear path such as:

    /captchapath/1.bmp

    3. Make a MYSQL table called "captcha" containing three fields:

    • Number
    • Image path
    • Answer

    Suppose you have 20 captcha images saved to your server. You should have a PHP script that will generate a random number from 1 to 20, and then fetch the image path corresponding to that number in the MySQL database.

    Typically you can have a script like this:

    <?php

    //connect database

    $username = "xxx";

    $password = "xxx";

    $hostname = "xxx";

    $database = "xxx";

    $dbhandle = mysql_connect($hostname, $username, $password)

    or die("Unable to connect to MySQL");

    //select a database to work with

    $selected = mysql_select_db($database,$dbhandle)

    or die("Could not select $database");

    echo 'Valid Email <br />';

    echo '<input type="text" name="rty" size="50">';

    echo '<br /><br />';

    echo 'Enter captcha (Case sensitive for security measure) <br />';

    echo '<input type="text" name="cvd" size="50">';

    echo '<br /><br />';

    //generate captcha

    $random= rand(1,12);

    $random = mysql_real_escape_string(stripslashes($random));

    $result2 = mysql_query("SELECT `imagepath` FROM `captcha` WHERE `number`='$random'")

    or die(mysql_error());

    $row = mysql_fetch_array($result2)

    or die("Invalid query: " . mysql_error());

    $captcha = $row['imagepath'];

    echo '<img src="'.$captcha.'"/>';

    echo '<br /><br /><input type="submit" /></form>';

    $random = mysql_real_escape_string(stripslashes($random));

    //extract answer of captcha

    $result3 = mysql_query("SELECT `answer` FROM `captcha` WHERE `number`='$random'")

    or die(mysql_error());

    $row = mysql_fetch_array($result3)

    or die("Invalid query: " . mysql_error());

    $answer = $row['answer'];

    //store answer to a session variable

    $_SESSION['captchaanswer']=$answer;

    ?>

    That concludes this tutorial. I hope you found it helpful. 



     
     
    >>> 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