pTutorial

New way to learn PHP Example

PHP Captcha Example

What is captcha?

Captcha is computer program that differentiate the human and computer. It is used to prevent spamming and automated data of website. Captcha test prevent the automated software to interacting servers.

PHP script to create the captcha

<?php 
error_reporting(1);
$a=rand(1,9);
$b=rand(1,9);

$r=$a+$b; 
$sum=$a."+".$b;
if(isset($_POST['z4']))
{
if($_POST['yu']==$_POST['j2'])
{
echo '<h3>'."HUMAN".'</h3>';
}
else
{
echo'<h3>'."Automated Script".'</h3>';
}
}
?>
<html>
<head>
<style>
h3
{
font-size:18px;
color:#AA0000;
}
</style>
</head

<form method="post">
<table border="1">
<tr>
<td> <?php
error_reporting(1);
echo $sum. " = ";
?>   <input type="hidden" name="j2" value="<?php echo $r; ?>"></td>
<td><input type="text" name="yu" size="3"><br></td>
</tr>
<tr >
<td colspan="2" ><input type="submit" name="z4" value="Submit"></td>

</tr>
</table>




</form>

</html>


In this example first create two random and store on variable $a and $b respectively and the range of random values are start from 1 to 9.

Create another variable that store the sum of random numbers that is $sum. Print the $sum variable that hold the string ($a "+" $b). Number will be shown on the screen separated by the + operator.

After that create a form and take the answer from user if the answer is correct then you are going to next stage otherwise you cannot pass this stage.

Output

1+3 =

QUOTES OF THE DAY

The ballot is stronger than bullet.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER