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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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
 

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


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

Dev Shed Tutorial Topics: