PHP

Captcha Image Verification

Captcha is a good way to avoid automatic form submission.In this tutorial i explains about captcha image verification. It create a image with  a random string displayed on it.  Then the user ask to fill the string in the  form filed and once form submitted it checks  if the string on image matches the input by the user . This ia easiest way to protect web form from spammers.
View Demo

Captcha.php
This file create an image with random string displayed on it.

<?php
session_start();
$text = rand(10000,99999);
$_SESSION["vercode"] = $text;
$height = 25;
$width = 65;
$image_p = imagecreate($width, $height);
$black = imagecolorallocate($image_p, 0, 0, 0);
$white = imagecolorallocate($image_p, 255, 255, 255);
$font_size = 14;
imagestring($image_p, $font_size, 5, 5, $text, $white);
imagejpeg($image_p, null, 80);
?>

This script genrate a random number from 10000 to 99999 and assign it to a session variable $_SESSION[“vercode”]. then it generate a image 25*65 px with balck background and font size 14px. Now create a web form .

<form name="captcha" method="post">
Name :<input type="text" name="name" required="required" />
Email :<input type="email" name="email" required="required" />
Verification code :<input type="text" name="vercode" size="10" required="required" />&nbsp;<img src="captcha.php">
<input type="submit" name="submit" value="Submit" />

Now create php script for submitting the form

<?php
session_start();
if ($_POST["vercode"] != $_SESSION["vercode"] OR $_SESSION["vercode"]=='')  {
     echo  '<strong>Incorrect verification code.</strong>';
} else {
     // add form data processing code here
     echo  '<strong>Verification successful.</strong>';
};
?>

This code check the verfication code input by user matches with sring given on image. if the code mataches then the form data submit else error message shows
View Demo

Download Source Code(Captcha Image Verification)
Size: 2.07 KB
Version: V 1.0

Recommended Tutorials for you


Related posts

Types of Errors in PHP

Anuj Kumar

How to change Password in php

Anuj Kumar

How to Salt & Hash a Password using Sha256

Anuj Kumar

How to Concatenate two or more string in PHP

Anuj Kumar

How to get Current Indian time in PHP

Anuj Kumar

PHP Variables

Anuj Kumar

How to prevent Cross-Site Request Forgery (CSRF) in PHP

Anuj Kumar

24 comments

Owolabi TOSIN EMMANUEL December 18, 2018 at 5:04 pm

I am learning so much from you

Reply
Rajshekar N s November 10, 2018 at 5:38 am

Sir how to change banner image in car rental project

Reply
Rajshekar N s November 10, 2018 at 5:53 am

Snd code to change banner image in car rental project Sir plz
rshekarns@gmail.com

Reply
Anuj Kumar November 10, 2018 at 6:14 am

make change in css file

Reply
Rajshekar N s November 10, 2018 at 6:33 am

I’m not getting Sir wt to change in tht

Reply
Rajshekar N s November 10, 2018 at 6:44 am

Which one shld I change in ths sir…

Anuj Kumar November 10, 2018 at 9:02 am

location of css assests/css/style.css
in style.css make change in this line
background-image: url(“../images/banner-image.jpg”)

Rajshekar N s December 1, 2017 at 5:55 pm

Sir how to change banner image in car rental project

Reply
Rajshekar N s December 1, 2017 at 5:57 pm

Snd code to change banner image in car rental project Sir plz
rshekarns@gmail.com

Reply
Anuj Kumar December 2, 2017 at 2:13 am

make change in css file

Reply
Rajshekar N s December 2, 2017 at 4:22 pm

I’m not getting Sir wt to change in tht

Reply
Rajshekar N s December 2, 2017 at 4:42 pm

Which one shld I change in ths sir…

Anuj Kumar December 2, 2017 at 8:23 pm

location of css assests/css/style.css
in style.css make change in this line
background-image: url(“../images/banner-image.jpg”)

Rajshekar N s December 3, 2017 at 5:23 am

I did bt it’s not changing sir

Anuj Kumar December 3, 2017 at 5:24 am

press ctrl+F5

Rajshekar N s December 3, 2017 at 5:27 am

No nothing hpn

Rajshekar N s December 3, 2017 at 5:32 am

C my code once
9 hours ago
location of css assests/css/style.css
in style.css make change in this line
background-image: url(“../images/banner-image-1.jpg”)

Anuj Kumar December 3, 2017 at 5:34 am

Can you give me your remote connection

Rajshekar N s December 3, 2017 at 6:02 am

I didn’t get u sir

Anuj Kumar December 3, 2017 at 6:05 am

check your mailbox

Rajshekar N s December 3, 2017 at 6:50 am

617356535

Rajshekar N s December 3, 2017 at 7:04 am

Sir 617356535

Rajshekar N s December 3, 2017 at 7:04 am

Sir 617356535

Rajshekar N s December 3, 2017 at 5:24 am

Only I need to change image name right.. …

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.