Create A Simple Captcha using PHP

Simon Ugorji
Geek Culture
Published in
4 min readOct 16, 2021

I recently created a custom image captcha using PHP, and I will show you how you can too.

OUR FILE STRUCTURE

  • Let’s start with the core folder.

We will create two files. One of them will generate the captcha image for us and the other will enable us to verify the generated captcha

Captcha_image.php

We need to generate our captcha text, hash it with md5(), then store the hashed value in a session for verification.

In our function, you may specify how many characters the user gets to fill out.

Now, there’s an interesting something I did with this script.

We will generate an image using PHP, create an array that will store the random background colors of the generated image, then we will pick a random color from the array.

Now we will use the function imagecolorallocate() to allocate the background color of our image, keeping in mind our array indexes.

Now we need to draw our image string using the function imagestring().

We will pass in the parameters;

  • $image — the generated image
  • 5 — Inbuilt font selection
  • 50 — the x coordinate of the upper left corner
  • 0 — the y coordinate of the upper left corner ( we need it to be centered )
  • $captcha_txt — the captcha text challenge
  • $img_clr — the captcha image color

Now, we need to set up an expiry time for the captcha text, hash the expiry time with the captcha text, then store it in a session variable.

Lastly, we need to convert the image string we had generated using imagestring() to a png image, then free any memory related to the image by destroying our image.

At this point, we are done with captcha_image file.

Let’s save the file now, and create a new one to verify our captcha.

Captcha_verify.php

We will declare a function that will accept just one parameter which is the user input, then set status codes as our return value.

STEP 1

Check if the 3 session variables exist, then assign their values to our local variables.

STEP 2

  • We will check if the captcha is still valid ( ie it hasn’t expired ).
  • Rehash the user input md5($input.$captcha_expire).
  • Then compare it with the one from our session.

If it matches, we will return a status code of 200 which means that our captcha verification was successful. But if it doesn’t match, we will return a status code of 400.

Now save the file and create an index page where we will display our captcha to the user

Index.php

Here are the things we will do

  • We will create an image tag and set the src to core/captcha_image.php.
  • We will create a form for the user to fill in the text from the image they see.
  • We will process the form input as soon as the user hits the submit button, then show the result of our verification to the user

Now, let’s test our script.

Now I will provide two different inputs and purposely leave the captcha unattended for 4 minutes so I will show you how the function works.

  • The Captcha Challenge
  • Failed Challenge
  • Successful challenge
  • Expired Challenge

ASSETS FOLDER

I have shown you the basic functions that we need. However, you may expand your captcha script to automatically switch images, in case the user can’t see through the current image.

You may also define some styling for your captcha.

Visit my GitHub Repo to see how I customized and styled mine.

Let me know if there’s anything related to this script I can help you out with!

Thank You!

--

--

Simon Ugorji
Geek Culture

Full-Stack Web Developer And Technical Writer On PHP & JavaScript