How to Create a Captcha Code in PHP

Captcha Code Very Important to Secure the Website Form..



Check Captcha Code Below Here..


Step-1:- Create a 2 Php Pages..

1-index.php
2- mostsimple.php




Step-2:- Write the Code of (index.php) page.

<?php
if(isset($_POST['check']))
{
echo "Form Submitted<br>";
echo "Name:".' '.$name=$_POST['name'];
}
$rand=substr(rand(),0,4);
?>
<style type="text/css">
.captcha
{
width:60px;
background-image:url(cat.png);
font-size:20px;
border: 1px solid;
}
.color
{
color:#FF0000;
}
</style>
<form method="post" action="<?php $_SERVER['PHP_SELF']?>" name="form1">
<table width="400px" align="center" bgcolor="#EBEBEB">
<tr>
<td>Name:</td>
<td><input type="text" name="name">
<span id="name" class="color"></span>
</td>
</tr>
<tr>
<td>Enter Captcha</td>
<td><input type="text" name="chk" id="chk">
<span id="error" class="color"></span>
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input type="text" value="<?=$rand?>" id="ran" readonly="readonly" class="captcha">
<input type="button" value="Referesh" onclick="captch()" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="submit" name="check" onclick="return validation();">
</td>
</table>
</form>
<script type="text/javascript">
function captch() {
    var x = document.getElementById("ran")
    x.value = Math.floor((Math.random() * 10000) + 1);
}
function validation()
{
if(document.form1.name.value=="")
{
document.getElementById("name").innerHTML="Enter your Name!";
document.form1.name.focus();
return false;
}
if(document.form1.chk.value=="")
{
document.getElementById("error").innerHTML="Enter Captcha!";
document.form1.chk.focus();
return false;
}
if(document.form1.ran.value!=document.form1.chk.value)
{
document.getElementById("error").innerHTML="Captcha Not Matched!";
document.form1.chk.focus();
return false;
}
return true;
}
</script>




Step-3:- Write the Code of (mostsimple.php) page.


<?php
if(isset($_POST['check']))
{
$one=$_POST['code'];
$two=$_POST['chk'];
if($one==$two)
{
echo "yes";
}
else
{
echo "no";
}
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']?>">
<?php
$rand=substr(rand(),0,4);
?>
<input type="text" name="code">
<input type="text" value="<?=$rand?>" id="ran" readonly="readonly">
<input type="hidden" name="chk" value="<?=$rand?>">
<input type="button" value="Referesh" onclick="captch()" />
<input type="submit" name="check">
</form>
<script type="text/javascript">
function captch() {
    var x = document.getElementById("ran")
    x.value = Math.floor((Math.random() * 10000) + 1);
}
</script>



Thanks..



Comments

Popular posts from this blog

How to Create a Login With Signup form in php

Code of the filter Drop-down Menu.

Create a Responsive mega Drop Down.