PHP- Captcha Example | How to create

//three files in same folder test.php,generate.php,arial.ttf


test.php

<?php
session_start();


if(isset($_POST['submit'])){

if(isset($_POST['t'])&& ($_POST['t']==$_SESSION['secure'])){
   echo "Match found";
   }else{
   echo "No Match try again!!!";
   $_SESSION['secure']=rand(1000,9999);
   echo'<img src="generate.php"/>';
   }
}
else{

$_SESSION['secure']=rand(1000,9999);

?>
<br/>
<img src="generate.php"/>
<?php
}
?>
<form action="test.php" method="post">
<input type="text"name="t"/>
<input type="submit" value="check" name="submit"/>
</form>



generate.php

<?php
session_start();
$text=$_SESSION['secure'];//from test.php
$font_size=30;
header('content-type:image/jpeg');

//Total Five image function used
$image=imagecreate(300,40);//imagecreate(widht,height)

imagecolorallocate($image,255,255,255);//image color is white now

$text_color=imagecolorallocate($image,0,0,0);//image text color is black now

//imagettftext($iBackground, $fontSize, $fontRotation, $fposx, $fposy, $color, $font, $string);
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'arial.ttf', $text);
//arial.ttf    c:/windows/font/arial.ttf
for($x=1;$x<=30;$x++){
    $x1=rand(1,100);
    $x2=rand(1,100);
    $y1=rand(1,100);
    $y2=rand(1,100);
   
   imageline($image, $x1, $y1, $x2, $y2, $text_color);
}

imagejpeg($image);
?>


Reactions: