PHP - Create Captcha Image

In this tutorial we will create a Create Captcha Image using PHP. This code will create a captcha image that needed to be solve by the user. The code use imagettftext() a php built-in tool that generate image data manually that write the captcha code and store the data to php SESSION. This is a user-friendly kind of program feel free to modify it. We will be using PHP as a scripting language and interpreter that is used primarily on any webserver including xamp, wamp, etc. It is being use to any famous websites and it has a modern technology that can easily be use by the next generation.

Getting Started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html. And, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating The Interface

This is where we will create a simple form for our application. To create the forms simply copy and write it into your text editor, then save it as shown below. index.php
  1. <!DOCTYPE html>
  2. <?php session_start()?>
  3. <html lang="en">
  4.         <head>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  6.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7.         </head>
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Create Captcha Image<h3/>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-3"></div>   
  19.                 <div class="col-md-6">
  20.                         <a class="btn btn-success" href="image.php">Create Captcha Image</a>
  21.                         <br />
  22.                         <br />
  23.                         <img src="images/captcha.jpg" />
  24.                 </div> 
  25.         </div>
  26. </body>
  27. </html>
image.php
  1. <!DOCTYPE html>
  2. <?php session_start()?>
  3. <html lang="en">
  4.         <head>
  5.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1" />
  6.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  7.         </head>
  8. <body>
  9.         <nav class="navbar navbar-default">
  10.                 <div class="container-fluid">
  11.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  12.                 </div>
  13.         </nav>
  14.         <div class="col-md-3"></div>
  15.         <div class="col-md-6 well">
  16.                 <h3 class="text-primary">PHP - Create Captcha Image<h3/>
  17.                 <hr style="border-top:1px dotted #ccc;"/>
  18.                 <div class="col-md-3"></div>   
  19.                 <div class="col-md-6">
  20.                         <form action="" method="POST">
  21.                                 <h3>Solve Captcha</h3>
  22.                                 <center><img src="captcha.php" /></center>
  23.                                 <br />
  24.                                 <?php include'solve.php'?>
  25.                                 <div class="form-group">
  26.                                         <input type="number" min="0" class="form-control" name="captcha" required="required"/>
  27.                                         <center><button class="btn btn-primary" name="solve">Solve</button></center>
  28.                                 </div>
  29.                         </form>
  30.                 </div> 
  31.         </div>
  32. </body>
  33. </html>

Creating the Main Function

This code contains the main function of the application. This code will create and display a captcha image when the button is clicked. To make this just copy and write these block of codes below inside the text editor, then save it as shown below. solve.php
  1. <?php
  2.         if(ISSET($_POST['solve'])){
  3.                 $captcha = $_POST['captcha'];
  4.                 if($_SESSION['captcha'] == $captcha){
  5.                         echo "<center><label class='text-success'>Congaratulation! You solve the captacha</label></center>";
  6.                 }else{
  7.                         echo "<center><label class='text-danger'>Invalid captcha!</label></center>";
  8.                 }
  9.         }
  10. ?>
captcha.php
  1. <?php
  2.         session_start();
  3.        
  4.         $random = rand(1, 9).rand(1, 9).rand(1, 9).rand(1, 9);
  5.        
  6.         $_SESSION['captcha'] = $random;
  7.        
  8.         $captcha = imagecreatefromjpeg("images/captcha.jpg");
  9.         $color = imagecolorallocate($captcha, 0, 0, 0);
  10.         $font = realpath('code.otf');
  11.         imagettftext($captcha, 20, 0, rand(30, 180), rand(20, 70), $color, $font, $random );
  12.         imagepng($captcha);
  13.         imagedestroy($captcha);
  14. ?>
There you have it we successfully created Create Captcha Image using PHP. I hope that this simple tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!

Add new comment