PHP Captcha Code

In this tutorial I want explain how to create a Captcha in PHP. We are using some of the features available in PHP for creating an image. This is very simple and basic tutorial and we are not using any custom fonts for generating captcha image. And we know that captcha code used to avoid spam/abuse or auto-submission.

Captcha Code

 





Download
Demo

Captcha.php

<?php
session_start();
$ranStr = md5(microtime());
$ranStr = substr($ranStr, 0, 6);
$_SESSION[‘cap_code’] = $ranStr;
$newImage = imagecreatefromjpeg(“cap_bg.jpg”);
$txtColor = imagecolorallocate($newImage, 0, 0, 0);
imagestring($newImage, 5, 5, 5, $ranStr, $txtColor);
header(“Content-type: image/jpeg”);
imagejpeg($newImage);
?>

Verifying captcha code is equal or not
Here we are storing a captcha code in SESSION variable and while verifying we have to compare the session variable with user entered data.
$_SESSION[‘cap_code’] – is having actual captcha code
$_POST[‘captcha’] – user entered captcha code

<?php
if ($_SERVER[‘REQUEST_METHOD‘] == ‘POST‘)
{
if ($_POST[‘captcha’] == $_SESSION[‘cap_code’])
{
// Captcha verification is Correct. Do something here!
}
else
{
// Captcha verification is wrong. Take other action
}
}
?>

Read This

The below html/CSS/Jquery code I used is just for an extra enhancement only and all the code is not needed actually. The above code is enough to check whether Human Verification is correct or wrong.

index.php
Contains HTML and PHP code. Image scr=’captcha.php

<?php
session_start();
$cap = ‘notEq’; // This php variable is passed to jquery variable to show alert
if ($_SERVER[‘REQUEST_METHOD’] == ‘POST‘)
{
if ($_POST[‘captcha’] == $_SESSION[‘cap_code’])
{
// Captcha verification is Correct. Do something here!
$cap = ‘Eq’;
}
else
{
// Captcha verification is wrong. Take other action
$cap = ”;
}
}
?>
<html>
<body>
<form action=”” method=”post”>
<label>Name:</label><br/>
<input type=”text” name=”name” id=”name“/>
<label>Message:</label><br/>
<textarea name=”msg” id=”msg“></textarea>
<label>Enter the contents of image</label>
<input type=”text” name=”captcha” id=”captcha” />
<img src=’captcha.php‘ />
<input type=”submit” value=”Submit” id=”submit“/>
</form>
<div class=”cap_status“></div>
</body>
</html>

Javascript

<script type=”text/javascript” src=”http://ajax.googleapis.com/
ajax/libs/jquery/1.4.2/jquery.min.js”></script>
<script type=”text/javascript“>
$(document).ready(function()
{
$(‘#submit’).click(function()
{
var name = $(‘#name’).val();
var msg = $(‘#msg’).val();
var captcha = $(‘#captcha’).val();
if( name.length == 0)
{
$(‘#name’).addClass(‘error’);
}
else
{
$(‘#name’).removeClass(‘error’);
}
if( msg.length == 0)
{
$(‘#msg’).addClass(‘error’);
}
else
{
$(‘#msg’).removeClass(‘error’);
}
if( captcha.length == 0)
{
$(‘#captcha‘).addClass(‘error’);
}
else
{
$(‘#captcha’).removeClass(‘error’);
}
if(name.length != 0 && msg.length != 0 && captcha.length != 0)
{
return true;
}
return false;
});
var capch = ‘<?php echo $cap; ?>‘;
if(capch != ‘notEq’)
{
if(capch == ‘Eq’)
{
$(‘.cap_status‘).html(“Your form is successfully Submitted “).fadeIn(‘slow’).delay(3000).fadeOut(‘slow’);
}
else
{
$(‘.cap_status‘).html(“Human verification Wrong!“).addClass(‘cap_status_error’).fadeIn(‘slow’);
}
}
});
</script>

CSS

body{
width: 600px;
margin: 0 auto;
padding: 0;
}#form{
margin-top: 100px;
width: 350px;
outline: 5px solid #d0ebfe;
border: 1px solid #bae0fb;
padding: 10px;
}#form label
{
font:bold 11px arial;
color: #565656;
padding-left: 1px;
}#form label.mandat
{
color: #f00;
}

#form input[type=”text“]
{
height: 30px;
margin-bottom: 8px;
padding: 5px;
font: 12px arial;
color: #0060a3;
}

#form textarea
{
width: 340px;
height: 80px;
resize: none;
margin: 0 0 8px 1px;
padding: 5px;
font: 12px arial;
color: #0060a3;
}

#form img
{
margin-bottom: 8px;
}

#form input[type=”submit“]
{
background-color: #0064aa;
border: none;
color: #fff;
padding: 5px 8px;
cursor: pointer;
font:bold 12px arial;
}

.error
{
border: 1px solid red;
}

.cap_status
{
width: 350px;
padding: 10px;
font: 14px arial;
color: #fff;
background-color: #10853f;
display: none;
}

.cap_status_error
{
background-color: #bd0808;
}

 

16 Comments

  1. Johnb158 said:

    Someone essentially help to make seriously posts I would state. This is the very first time I frequented your web page and thus far? I surprised with the research you made to make this particular publish incredible. Magnificent job! ecdgdeakacbf

    June 6, 2016
    Reply
  2. Johnd593 said:

    Thanks for another magnificent post. Where else may just anybody get that kind of info in such a perfect manner of writing? I have a presentation next week, and I’m at the look for such information. fdkagecbgddd

    June 6, 2016
    Reply
    • Ladainian said:

      Glorious golden sky, Drowsey! I love itI#3.&9;m not home right now because I'm on the west coast visiting friends and relatives, so I've used a yellow mystery plant from my files. Maybe someone will know what it is.– K

      June 28, 2016
      Reply
  3. Bubi said:

    Nice read, I just passed this onto a friend who was doing a little research on that. And he just bought me lunch because I found it for him smile Therefore let me rephrase that: Thank you for lunch! &#ori0;Cu228sity will conquer fear even more than bravery will.” by James Stephens.

    June 28, 2016
    Reply
  4. Johnc659 said:

    Because here is a list of multiplayer games is that the leave was asked for more. fcffeeeeeagg

    August 24, 2016
    Reply
  5. Johne3 said:

    Good day! I know this is kind of off topic but I was wondering if you knew where I could find a captcha plugin for my comment form? I’m using the same blog platform as yours and I’m having problems finding one? Thanks a lot! cbdefkbcdega

    August 25, 2016
    Reply
  6. Johna864 said:

    Appreciate it for helping out, great information. dacfdabeedkb

    September 16, 2016
    Reply
  7. Johnd155 said:

    I really like your writing style, fantastic information, thanks for putting up eefeaafefafc

    November 3, 2016
    Reply
  8. Johne925 said:

    I am in agreement with pretty much everything that you mentioned entirely! Excellent website document! ecdddcgeeafc

    August 13, 2017
    Reply
  9. Smithe576 said:

    It’s actually a great and helpful piece of information. I am glad that you just shared this useful information with us. Please stay us up to date like this. Thank you for sharing. acgdgdeeaffefdek

    August 13, 2017
    Reply
  10. Google said:

    Google

    We came across a cool site that you simply may possibly take pleasure in. Take a search in case you want.

    August 26, 2017
    Reply
  11. Hello.This article was extremely fascinating, especially since I was investigating for thoughts on this subject last Sunday.

    September 5, 2017
    Reply
  12. captchaboss said:

    Gгeetings fгom California! I’m bored at work
    so I decided tⲟ browse ʏour site on mу iphone duгing lunch break.
    Ӏ reаlly like the knowledge үou present here аnd can’t wait to takе a
    looк whеn I gеt һome. Ι’m amazed at hоw fast your blog loaded ⲟn my phone ..
    I’m not еᴠen using WIFI, just 3G .. Аnyhow, very ɡood site!

    December 13, 2017
    Reply
    • Prince Joal said:

      Thankyou You

      December 18, 2017
      Reply
  13. iveyhendrickson said:

    Ⅴery nice article. I definiteⅼү love tһis
    site. Continue the ցood ԝork!

    January 1, 2018
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

code