Search This Blog

10 February 2018

CREATE CAPTCHA CODE IN PHP



CREATE CAPTCHA CODE IN PHP

What is Captcha?
CAPTCHA=Completely Automated Public Turing test to tell Computers    and Humans Apart.Captcha is one way to Secure Your Website But not Only

Advantages:-
      Protecting Your Registeration Form
    Preventing Spam Comments
)   Online Shopping More Secure
Protect Email Accounts

First You Create Image Using PHP

<?php

session_start();
$chars="ABCDEFGHIJAKLMNOPQabcdefghijklamnopq1234567890";
$cap=substr(str_shuffle($chars),0,6);
$_SESSION['captcha']=$cap;

$image = imagecreate(100, 20);
$background = imagecolorallocate($image, 128,128,128);
$foreground = imagecolorallocate($image, 255,255,255);
imagestring($image,5,5,1,$cap,$foreground);
header("content-type:image/jpeg");
imagejpeg($image);
imagedestroy($image);
$size=getimagesize($image);
?>
/** Number Captcha  */
<?php

session_start();
$a=rand(1,20);
$b=rand(1,22);
$total=$a+$b;
$math = "$a"." + "."$b"." =";

$_SESSION['captcha'] = $total;
$background = imagecolorallocate($image, 128,128,128);
$foreground = imagecolorallocate($image, 255,255,255);
imagestring($image,15,55,15,$math,$foreground);
header("content-type:image/jpeg");
imagejpeg($image);
imagedestroy($image);
$size=getimagesize($image);

$image = imagecreate(200, 50);




?>
Next Create Your Registeration Form With Captcha


<html>
<head> </head>
<title> Captcha </title>
<link rel="icon" href="/img/chennai.jpeg">
<style>
.col1{
    height:100%;
    width:100%;
    position:relative;
   
}
.col2{
    position:absolute;
    width:25%;
    height:35%;
    background-color:#e9e6e6;
    margin-top: 20%;
    margin-left: 35%;
   
}

font-family: "Helvetica Neue",Helvetica,Arial,sans-serif;
    font-size: 14px;
    line-height: 1.42857143;
    color: #333;
.fm{
margin-left:20%;   
}
.inpt{
    height: 28px;
    padding:6px 12px;
    width:80%;
    border: 1px solid #ccc;
    border-radius: 4px;
    color:#555;
}
.btn{
border: 1px solid transparent;
border-radius: 4px;
height: 11%;
width: 42%;
background-color: #ED6D62;
color:white;
    left: 81px;
    position: absolute;
}
img{
    padding-left: 32px;

margin-right: -15px;
    margin-left: -15px;
    margin-top: 0px;
    margin-bottom: 0px;
    padding-top: 0px;
    padding-bottom: 0px;
    padding-left: 0px;
    padding: 0px;
    }

</style>
<body>
<div class=" col1 ">
    <div class=" col2 ">
    <br>
        <form method=" post " action="" class="fm">
       
                <input type =" text " class =" inpt " name=" reg " placeholder = " Enter Your Regno" required> <br>
                <br>
                <input type=" text " class=" inpt " name=" name " placeholder=" Enter Your Name " required> <br>
                <br>
                <input type=" text " class=" inpt " placeholder=" Enter Your city " name=" cy ">    <br>
                <br>
                <input type=" text " class=" inpt " placeholder=" Enter Your Captcha " name=" cap " required>    <br>
                <br>
                <img src=" /img/captcha.php " alt=" php captcha image"><br>
                 <br>
                 <br>
            <input type=" submit " name=" sub " class=" btn " value="check">
        </form>
    </div>
</div>
</body>
</html>
<?php
session_start();
$captcha=$_SESSION['captcha'];
if(isset($_POST['sub'])){
    $regno =$_POST['reg'];
    $name =$_POST['name'];
    $city =$_POST['cy'];
    $cap =$_POST['cap'];
if($captcha == $cap){
echo "<script> alert (' All inputs are Valid '); </script>";
                }
else{
echo "<script> alert (' All inputs are NOT Valid '); </script>";   
    }   
   
}
?>


OutPuts:












2 comments:

  1. am really happy to see that anti captcha key is really informative and this is a great inspiring article.

    ReplyDelete
  2. They provided a comprehensive sitemap and navigation plan. fort worth web design

    ReplyDelete

Jquery or Javascript Start Exam Time

 <script> function startTimer() {      var date = "<?php echo $date ?>"; // dynamic date      var time = "<?...