Home >>Advance PHP Tutorial >PHP CAPTCHA

PHP CAPTCHA

Create CAPTCHA image PHP

A CAPTCHA is a type of challenge-response test used in computing as an attempt to ensure that the response is generated by a human being. It is an acronym based on the word "capture" and standing for "Completely Automated Public Turing test to tell Computers and Humans Apart.

Create Arithmetic CAPTCHA

<?php

error_reporting(1);

$arr=range(99,9);

$brr=range(99,9);

$randa=array_rand($arr);

$randb=array_rand($brr); 

$a=$arr[$randa];

$b=$brr[$randb];

$r=$a+$b; 

$cap=$a."+".$b;

if(isset($_POST['b1']))

 {

    if($_POST['t2']==$_POST['t3'])

    {

    echo '<center>'.'<font color="blue" size="5">'."Welcome user".'</font>'.'</center>';

    }

   else

   {

   echo '<center>'.'<font color="red" size="5">'."Please fill the correct answer".'</font>'.'</center>';

   }

}

?>


   <html>

    <center>

    <form method="post">

<?php

error_reporting(1);

echo $cap."=";

?>

<input type="hidden" name="t3" value="<?php echo $r; ?>">

<input type="text" name="t2" autofocus><br>

<input type="submit" name="b1" value="match">

</form>

</center>

</html>
 
Output Welcome user 30 + 42=
In the above example First create a variable $arr with multiple values i.e created by range function(using array range( ) function ). second variable $brr also hold multiple values between "9" to "99". Now pass $arr inside array_rand( ) function, it returns a random array index which stores in $randa and $randb variable. Now call the value of array $arr[$randa] and store it in $a and $b variable. In next line create a variable $cap which holds a string ($a."+".$b) print the $cap variable it shows two arithmetic numbers seperated by "+". Now calculate the value, enter the calculated value in text-box, to check the addition of two numbers is correct or not. if addition of two numbers entered by user is right it shows a message "Welcome user" otherwise show error message "Please fill the correct answer".

PHP Create CAPTCHA Security Images

<form action="" method="post" >

  <table  border="0" align="center">

    <tr>

      <td width="215"> Validation code:</td>

      <td width="162">

<?php

$arr= array_merge(range(0,9),range("A","Z"));

//print_r($arr);

for($i=1;$i<=5;$i++)

{
	$ch = $arr[array_rand($arr)];

	@$captcha=$captcha.$ch;

	@$fc=$fc.$ch.",";

}

//echo $fc."<br>";
$nar = explode(",",$fc);

for($i=0;$i<5;$i++)

{

echo $nar[$i];
//echo "<img src='$nar[$i].GIF'/>";

}

if(isset($_POST['match']))

{

    if($_POST['img']==$_POST['hid'])

     {

      echo  "<br/><font color='blue'>security code matched</font>";

     }

    else

    {

	echo "<br/><font color='red'> try again</font>";

    }

}

?>

    <tr>

        <td>Enter the  above code here :</td>

       <td> <input name="img" type="text">

        </td>

    </tr>

    <tr>

      <td colspan="2" align="center">

	  <input name="match" type="submit"  value="Submit Security code"></td>

    </tr>

  </table>

<input type="hidden" value="<?php echo $captcha; ?>" name="hid"/>

</form>

</pre>

</div><br/>

Output captcha

No Sidebar ads