A big problem today is spam.
The best way to combat spam is Captcha. Captcha makes the difference between a computer and a human being. Computers cannot read the text in a picture and this is why we’ll use a photo too.

Let’s start by creating the file captcha.php. This file will create a photo with a text written on it.

Let’s save the string written on the image in a cookie. We will read this cookie once a form is submitted to compare data.
setcookie("cookie-captcha", (md5($rand_string)), time()+3600, "/");
imagejpeg($my_image); // Create the jpeg picture
imagedestroy($my_image); // Delete the jpeg picture

In this example the cookie’s name is cookie-captcha, it’s content will be the md5 coded value of the random string obtained earlier (the number written in the image), it will be valid for an hour on all your domain.

Now that the image is created and the value is saved in the cookie it’s time to validate the data users enter.
The picture created by the file captcha.php will be inserted in the html code like any other image: