In this post I want explain how to create and implement a Captcha in PHP. We are using inbuilt 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. Captcha code used to avoid spam/abuse or auto-submission.  The random value of this image appears inside the generated CAPTCHA image which can be refreshed if not readable. This Captcha Code you can implement on the website level as well on any specific page like contact form.

Live Demo of PHP Captcha and source code download free –

Demo  Download Source

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);
?>

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
captcha-php
 

< ?php
session_start();
$cap = 'notEq';
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
    }
}
?>

Complete Captcha working code:

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

    
        
        Captcha - PHP | Jquery
        
        
        
    
    
	
	
	

PHP Captcha Code

Online Web Applications is a global community dedicated to provide web technology archive | News and tutorials focused on Technology, Social Media, Blogging, Tutorials, Software and WordPress

More similar post and tutorials onlinewebapplication.com


Not readable? Change text.
Close Bitnami banner