PHP Captcha

PHP Captcha is used to protect forms from unwanted spam access and abuse. CAPTCHA is a randomly generated code. It is produced in run time.

Types of Captcha are as given below.

1. Text

2. Graphical

3. Audio

 

How to Create Captcha in PHP?

First create captcha.php which is used to generate a captcha image using the GD library as below example.

Then create contact us form as below example.

Check correct captcha code as below.

 if($_POST["captcha"]!=""&&$_SESSION["code"]==$_POST["captcha"]){

           echo "You have  entered correct captcha!";    

 }else{
           echo "Please enter correct captcha!";    
  }

 

Example :

<?php
session_start();
$code=rand(1000,9999);
$_SESSION["code"]=$code;
$im = imagecreatetruecolor(50, 24);
$bg = imagecolorallocate($im, 22, 86, 165); //background color blue
$fg = imagecolorallocate($im, 255, 255, 255);//text color white
imagefill($im, 0, 0, $bg);
imagestring($im, 5, 5, 5, $code, $fg);
header("Cache-Control: no-cache, must-revalidate");
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
?>


// Create contact us form as below

<form class="form-inline contact_form" method="post" action="" data-toggle="validator">

<div class="form-group contact_form_group">
<label class="input_heading col-md-4">Name: </label>
<input name="name" type="text" class="form-control contact_form_input" id="name" placeholder="Your Name" required>
</div>
<div class="form-group contact_form_group">
<label class="input_heading col-md-4">Email: </label>
<input name="email" type="text" class="form-control contact_form_input" id="email" placeholder="Your Email Id" required>
</div>
<div class="form-group contact_form_group">
<label class="input_heading col-md-4">Phone:</label>
<input name="phone" type="double" class="form-control contact_form_input" id="phone" placeholder="Your Mobile Number">
</div>


<div class="form-group contact_form_group">
<label class="input_heading col-md-4">Please Enter the captcha:</label>
<input name="captcha" type="text" placeholder="Enter the Captcha" class="form-control contact_form_input" required>
<img src="<?=$weburl ?>/captcha.php" width="50" height="24" class="form-control"/>
</div>


<div class="form-group contact_form_group text-center">
<button type="submit" class="btn btn-warning" name="send">Send</button>
</div>
</form>

Output :

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

18182