Projects Personal Contact Rss
 


Simple Captcha generation in PHP

A Simple PHP captcha system. In order to use this code you simply need to include save the captcha2.php file somewhere in your web tree, create a session variable to store the string you want as your captcha text and provide an img link to the captcha.

<?php
 $_SESSION['CAPTCHA_STRING'] = 'random';
 $_SESSION['CAPTCHA_COLOUR'] = 'blue,red,yellow,green';
?>
<img src = 'captcha2.php' alt='Security captcha' border=0>
<br>Enter the text requested in the above image:
 <input type='text' name='form_captcha' value=''>

This will produce the following output in your page:
Security captcha
Enter the text requested in the above image:

The code to check the text against the captcha when you are handling your post is remarkably simple:

if ($_POST['form_captcha'] === $_SESSION['CAPTCHA_STRING']) {
  // it is good...
} else {
  // sorry, bad answer
}


You can download the source code for captcha2.php and its required library pg_gd_wrappers.inc.php from here:



All content © Paul Gregg, 1994 - 2026
Page last updated: 30 March 2026 18:19:53.
This site https://pgregg.com has been online since 5th October 2000
Previous websites live at various URLs since 1994