IMAGE CAPTCHA CODE -


-------------captchapage.php-----------


<!DOCTYPE html>

<html lang="en">

<head>

<!-- http://thepiggery.net/captcha/fonts/ -->

<meta charset="UTF-8">

<meta name="viewport" content="width=device-width, initial-scale=1.0">

<title>Text captch example</title>

</head>

<body>

<h1><center>TEXT CAPTCHA</center></h1>

<hr>

<form action="testcode.php" method="post">

<p>Enter the email : <input type="text" name="email"/></p>

<p>Enter the Captcha :

<img src="captcha.php"><br/>

<input type="text" name="captch"/>

</p>

<p><input type="submit" name="submit"/></p>

</form>

</body>

</html>


----------captcha function -------------

CAPTCHA FUNCTION _


<?php

$mattrix = [chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

chr(rand(65,90)),rand(0,9),chr(rand(97,122)),

];

function get_captcha_text($size=6){

global $mattrix;

$c_arr = [];

for ($i=0; $i<$size; $i++){

$c_arr[$i] = $mattrix[rand(0,count($mattrix)-1)];

}

$captcha = join($c_arr);

return $captcha;

}




//echo get_captcha_text();

?>

 


------------captcha code.php------------


<?php

require_once __DIR__.'/captchafun.php'; // captcha function called

header("Content-Type:image/jpeg");

$width=150;

$height=50;

$lines = rand(10,20);

//image frame create --background size

$image_frame = imagecreate($width, $height);

$r1= rand(0,255);

$g1= rand(0,255);

$b1= rand(0,255);

//layering -1 - bacground color

imagecolorallocate($image_frame,$r1,$g1,$b1);

$r2= rand(0,255);

$g2= rand(0,255);

$b2= rand(0,255);

//layering -2 - text color (foreground)

$color = imagecolorallocate($image_frame,$r2, $g2,$b2);

$r3= rand(0,255);

$g3= rand(0,255);

$b3= rand(0,255);

$line_color = imagecolorallocate($image_frame,$r3, $g3,$b3);

for ($i=0; $i<$lines; $i++) {

$x1 = rand(0,$width);

$x2 = rand(0,$width);

$y1 = rand(0,$height);

$y2 = rand(0,$height);

imageline($image_frame,$x1, $y1, $x2, $y2, $line_color);

}

$dir = __DIR__;

$font_file = "{$dir}/font17.ttf";

//$tb= imagettfbbox(20,10,$font_file,"Deepak");

session_start();

$captcha = get_captcha_text();

$_SESSION['captcha']=$captcha;

//processing of text

imagettftext($image_frame,20,5,10,40,$color,$font_file,$captcha);

//imagejpeg($image_frame,"capcha.jpg",100);

imagejpeg($image_frame);

// if you are writeing to file free memory

//imagedestroy($image_frame);

?> 





-----------test1.php--------


<?php

$email = $_POST['email'];

$captcha = $_POST['captcha'];

session_start();

$sess_capcha = $_SESSION['captcha'];

echo $sess_capcha;

?>