PHP Captcha System (Captcha with php GD) - Web Development and Web Design Codes

Latest

Tuesday, February 13, 2018

PHP Captcha System (Captcha with php GD)

PHP Captcha System

PHP Captcha System (Captcha with php GD)
PHP Captcha System (Captcha with php GD)

Hi Friends Today today we are going to learn how to create captcha system in php using php session and php GD..
Just follow the below example to learn php captcha system..

Create 2 files named 1. index.php and captcha.php

1. index.php 

<?php
session_start();
if(isset($_POST['submit'])) {
if($_POST["captcha"]==$_SESSION["captcha"]){
$message = "<b><font style=color:green size=4>Your Captcha successfully Validate</font>";
}
else{
$message = "<b><font style=color:red size=4>Enter Correct Captcha Code</font>";
}
}
?>
<html>
<head>
<style>
 input[type=text]{
  padding:4px;
  border-radius:4px;
 }
 input[type=submit]{
  cursor:pointer;
  padding:5px;
  border-radius:4px;
  background:black;
  color:white;
 }
</style>
<title>PHP Captcha using SESSION</title>
<h2>PHP Captcha using SESSION</h2>
 <form method="POST">
 <table>
     <tr>
     <td>Captcha</td>
  <td><img src="captcha.php"></img></td>
  </tr>
  
  <tr>
     <td>Captcha Codes:</td>
  <td><input type="text" name="captcha"/></td>
  </tr>
  
  <tr>
     <td></td>
  <td><input type="submit" name="submit"/></td>
  </tr>
  </table>
</html>
<?php if(isset($message)){
 echo $message;
}?>

2. captcha.php

<?php
session_start();
$random_alpha = md5(rand());
$captcha_code = substr($random_alpha, 0, 6);
$_SESSION["captcha"] = $captcha_code;
$target_layer = imagecreatetruecolor(70,30);
$captcha_background = imagecolorallocate($target_layer, 255, 160, 119);
imagefill($target_layer,0,0,$captcha_background);
$captcha_text_color = imagecolorallocate($target_layer, 0, 0, 0);
imagestring($target_layer, 5, 5, 5, $captcha_code, $captcha_text_color);
header("Content-type: image/jpeg");
imagejpeg($target_layer);
?>

That's It Guys how to create captcha code in php..Thank You..

No comments:

Post a Comment

Thank You for Your Comment