Home arrow PHP arrow Page 4 - Designing a Captcha System with PHP and MySQL

Captcha System Without GD Support - PHP

Spam is one of the biggest problems on the Internet. It is getting harder to fight with the advent of spam bots that visit websites and automatically fetch email addresses, fill out forms and do other nasty things, such as blog spam comments, that could degrade your integrity. Fortunately, using captcha can help. This article will show you how to implement captcha on your site.

  1. Designing a Captcha System with PHP and MySQL
  2. The Captcha Image Generation Script
  3. The PHP Form with Captcha-Generated Challenge
  4. Captcha System Without GD Support
By: Codex-M
Rating: starstarstarstarstar / 10
June 04, 2009

print this article



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:


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:


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



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

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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates


Dev Shed Tutorial Topics: