PHP captcha
<?php
//here we will create captcha..
session_start();
$rand_num=rand(1000,99999);
$_SESSION['code']=$rand_num;
//create image background image..
//gd library function..
//image area defined..
$l=imagecreatetruecolor(70,30);
//background color:
$captcha_bg=imagecolorallocate($l,255,160,120);
//layer and captch_bg attach
//image,x,y,color
imagefill($l,0,0,$captcha_bg);
//text color of captcha
$captca_text_color=imagecolorallocate($l,255,255,255);
//all conect layer,fontsize,x,y,random number,captca color
imagestring($l,5,5,5,$rand_num,$captca_text_color);
header("Content-Type:image/jpeg");
//image display
imagejpeg($l)
?>
<!-- Many DLL files are located in the extensions
https://www.codexworld.com/how-to/install-php-gd-library-windows-server/
https://stackoverflow.com/questions/4560996/call-to-undefined-function-imagecreatetruecolor-error-in-php-pchart
-->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js" ></script>
</head>
<body>
<form id="frm">
<label for="name">Name</label>
<input type="text" name="nm" id="nm">
<br/>
<label for="password">Password</label>
<input type="password" name="pwd" id="pwd">
<br/>
<div style="margin:20px;">
<img src="captcha.php">
<!-- <input type="reset" value="resets"> -->
</div>
<br/>
<label for="Enter captch">Enter Captcha</label>
<input type="text" name="valcaptcha" id="valcaptcha">
<!-- <button id="btn">submit</button> -->
<!-- <button id="btn" onclick="submit_data()">Submit</button> -->
<button id="btn" >Submit</button>
</form>
</body>
<script>
$(document).ready(function(){
$("#btn").click(function(e){
e.preventDefault();
console.log("button clicked");
nm=$("#nm").val();
pwd=$("#pwd").val();
valcaptcha=$("#valcaptcha").val();
$.ajax({
url:'insert.php',
type:'post',
data:{nm:nm,pwd:pwd,valcaptcha:valcaptcha},
success:function(data){
console.log(data);
}
})
})
})
/*function submit_data(e){
//e.preventDefault();
jQuery.ajax({
url:'insert.php',
type:'post',
data:jQuery("#frm").serialize(),
succcess:function(data){
alert(data);
//console.log("data");
}
})
}*/
/*document.getElementById("btn").addEventListener("click", function(event){
event.preventDefault();
console.log("buton has been clicked");
jQuery.ajax({
url:'insert.php',
//type:'post',
//data:jQuery("#frm").serialize(),
succcess:function(data){
alert(data);
//console.log("data");
}
})
});*/
</script>
</html>
<?php
session_start();
include "sql_connect.php";
$name=$_REQUEST['nm'];
//echo $name;
$pwd=$_REQUEST['pwd'];
echo $pwd;
$captcha=$_REQUEST['valcaptcha'];
$captchavl=$_SESSION['code'];
//echo $captchavl;
if($captcha==$captchavl){
//echo "submit captcha here";
$sql_query = "INSERT INTO captch_store (sno,name,pwd)
VALUES ('','{$name}','{$pwd}')";
//echo $sql_query;
$query = mysqli_query($con, $sql_query) or die("not executing");
echo "data inserted";
}
else{
echo "please enter valid captcha code";
}
//echo "hello this is gaurav";
?>
Comments
Post a Comment