Fri, 17 May 2024


Simple PHP Captcha Script Tutorial

By: Peter, NetArt Media
Wed, 1 August 2018
Simple PHP Captcha Script Tutorial

One of my main credos is 'Keep It Simple Stupid'. While developing in PHP can sometimes entail a lot of code, this doesn't always have to be so. Especially when it comes to something as simple as a captcha system.

No captcha script is completely safe from spam bots and other annoying or malicious interaction, however, this simple system has done myself and many others quite well. It at least keeps spam insertion down to a good degree.

There are 5 steps to creating the captcha script and I will go through all of them using images and code blocks where appropriate. Between the video and this article you should be coding up your own captcha script in no time. Or at least get some ideas for one.

Step 1 - Creating The Captcha Images



I'm a big fan of the image editor Gimp, which I used to create the 10 captcha images for this tutorial. Of course, if you end up creating a captcha system you are going to want a lot more than 10 images. I suggest from 50-100.

Yes, it's a bit of work and images can be created with PHP - which is something I will cover in a future tutorial. However, for the code in this script to be as simple as it is we need to create the images ourselves.

I made my captcha images 125px wide by 25px high, using a white background and a legible, black and bold Verdana font. Some spam bots can read images, sure. But that doesn't mean human beings should have a hard time reading them. Which is one of my biggest pet peeves with most captcha systems.

Step 2 - Creating and Importing The SQL File



We could have created a script for entering our information into the MySQL database table but I thought an introduction to creating a SQL file for importing our table into MySQL would be a helpful step toward learning a bit more about how PHP and MySQL work in unison.

There are several free applications online for creating SQL files and you could use one of those. Make sure the syntax is correct though, as MySQL will give you errors and not import the file otherwise.

To import a SQL file into MySQL choose the database where you want to import it to, use the 'choose file' field option and press 'Go'. It's that simple.

Step 3 - Displaying Captcha Image In HTML Form



To display the random captcha image in the form we use a basic Prepared SELECT Statement to get the id and image name from our MySQL table. A little bit of HTML wraps and defines our image, then we create a hidden input field where the id of the random image is embedded.

NOTE: Make sure you use autocomplete="off" within the captcha form field. You want people to have to type in the captcha code every time.

Step 4 - Processing and Validating The Code



Finally, we can process and validate the captcha code entered into the field. For this tutorial I created success and error messages, which will be displayed according to the processed outcome. If the captcha field is empty or the incorrect code is entered into the field a red error is shown. If the code is correct a blue success message is displayed.

1. We connect to the MySQL database and if we aren't connected we get an error.

2. Define the success and error messages before we redefine them within our Prepared and Conditional Statements.

3. Make sure the form is submitted and that the id matches the code that is stored alongside that particular id within the MySQL database table.

4. Assign our individual error messages according to the processed outcome.

This is a very simple, bare bones script that would only be part of processing an HTML form. However, the captcha system is certainly useful if things are coded properly. Be sure to refer to the video for added visual tutelage of this script.

I hope you can find this PHP captcha system useful.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox