up home page bottom

Add a comment

French German version Spanish version Italian version

A Quick and Dirty CAPTCHA Using PHP and the GD Library.

1 Star5 Stars (+1 rating, 1 votes)
Loading ... Loading ...

If you're just starting to get into PHP you may be a bit confused about how the GD Library works, and how to implement that into things such a CAPTCHA. This is a very easy implementation of a captcha. This will help stop spammers, and spam bots from abusing your form, or spamming your server.

I won't however, prevent the abuse as noted in Jeff Atwoods post, it will however help.

So, let's say you have a public submission or contact form on your website, that looks somewhat like the following:

HTML:
  1. <form method="post"> Contact us:
  2. <textarea cols="30" rows="5" name="simple_contact"></textarea>
  3. <input value="Submit" type="submit" />
  4. </form>

We'll use a captcha to keep this form somewhat safe ().

Obviously you need a PHP engine enabled for your Web server to execute PHP scripts, and GD (PHP graphics library) to generate the image. The solution below is tested for Apache (Windows and Unix), IIS (Windows), PHP-4, PHP-5, GD and GD2.

1) Make a PHP script (separate file captcha.php) which will generate the image:

PHP:
  1. header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
  2. header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
  3. header("Cache-Control: no-store, no-cache, must-revalidate");
  4. header("Cache-Control: post-check=0, pre-check=0", false);
  5. header("Pragma: no-cache");
  6.  
  7. function _generateRandom($length=6)
  8. {
  9. $_rand_src = array(
  10. array(48,57) //digits
  11. , array(97,122) //lowercase chars
  12. //        , array(65,90) //uppercase chars
  13. );
  14. srand ((double) microtime() * 1000000);
  15. $random_string = "";
  16. for($i=0;$i&lt;$length;$i++){
  17. $i1=rand(0,sizeof($_rand_src)-1);
  18. $random_string .= chr(rand($_rand_src[$i1][0],$_rand_src[$i1][1]));
  19. }
  20. return $random_string;
  21. }
  22.  
  23. $im = @imagecreatefromjpeg("captcha.jpg");
  24. $rand = _generateRandom(3);
  25. $_SESSION['captcha'] = $rand;
  26. ImageString($im, 5, 2, 2, $rand[0]." ".$rand[1]." ".$rand[2]." ", ImageColorAllocate ($im, 0, 0, 0));
  27. $rand = _generateRandom(3);
  28. ImageString($im, 5, 2, 2, " ".$rand[0]." ".$rand[1]." ".$rand[2], ImageColorAllocate ($im, 255, 0, 0));
  29. Header ('Content-type: image/jpeg');
  30. imagejpeg($im,NULL,100);
  31. ImageDestroy($im);
  32. ?&gt;

2) Add the following line at the top of the page where you need to implement CAPTCHA:

PHP:

3) Add the following line to check whether the CAPTCHA string entered by the visitor is valid, before the line where you will proceed with a submitted message:

PHP:
  1. if($_SESSION["captcha"]==$_POST["captcha"])
  2. {
  3. //CAPTHCA is valid; proceed the message: save to database, send by e-mail ...
  4. }
  5. ?&gt;

4) Finaly add the CAPTCHA to the form:

PHP:
  1. <form method="post">
  2. <table bgcolor="#cccccc">
  3. <tr>
  4. <th>Contact us (Post new message):</th>
  5. </tr>
  6. <tr>
  7. <td><textarea cols="30" rows="5" name="message"></textarea></td>
  8. </tr>
  9. <tr>
  10. <td align="center">CAPTCHA:
  11.  
  12. (antispam code, 3 black symbols)
  13. <table>
  14. <tr>
  15. <td><img src="captcha.php" alt="captcha image" /></td>
  16. <td><input name="captcha" size="3" maxlength="3" type="text" /></td>
  17. </tr>
  18. </table>
  19. </td>
  20. </tr>
  21. <tr>
  22. <th align="center"><input value="Submit" type="submit" /></th>
  23. </tr>
  24. </table>
  25. </form> if(isset($_POST["captcha"]))
  26. if($_SESSION["captcha"]==$_POST["captcha"])
  27. {
  28. //CAPTHCA is valid; proceed the message: save to database, send by e-mail ...
  29. echo 'CAPTHCA is valid; proceed the message';
  30. }
  31. else
  32. {
  33. echo 'CAPTHCA is not valid; ignore submission';
  34. }
  35. ?&gt;

There you go! A quick, and dirty captcha for easy implementation. Of course there are much more secure and stronger methods to achieve this feature, but this is just the quick and dirty.

Hope this helped.

Spread the Love: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • TwitThis
  • Technorati
  • del.icio.us
  • Facebook
  • Sphinn
  • Mixx
  • Google
  • Live
  • YahooMyWeb
  • blogmarks
  • BlogMemes
  • co.mments
  • Fark
  • feedmelinks
  • Gwar
  • Linkter
  • Netvouz
  • Smarking
  • Socialogs
  • Taggly
  • Yigg

Leave a Comment

Leave a comment or send a note
  1. (required)
  2. (valid email required)
  3. (required)
  4. Send
 

cforms contact form by delicious:days