PHP - Create Captcha Image

Submitted by: 
Language: 
Visitors have accessed this post 217 times.

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">PHP - Create Captcha Image<h3/>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-3"></div>
  20. <div class="col-md-6">
  21. <a class="btn btn-success" href="image.php">Create Captcha Image</a>
  22. <br />
  23. <br />
  24. <img src="images/captcha.jpg" />
  25. </div>
  26. </div>
  27. </body>
  28. </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="<a href="https://sourcecodester.com">Sourcecodester</a>
  12. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  13. </a> </div>
  14. </nav>
  15. <div class="col-md-3"></div>
  16. <div class="col-md-6 well">
  17. <h3 class="text-primary">PHP - Create Captcha Image<h3/>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <div class="col-md-3"></div>
  20. <div class="col-md-6">
  21. <form action="" method="POST">
  22. <h3>Solve Captcha</h3>
  23. <center><img src="captcha.php" /></center>
  24. <br />
  25. <?php include'solve.php'?>
  26. <div class="form-group">
  27. <input type="number" min="0" class="form-control" name="captcha" required="required"/>
  28. <center><button class="btn btn-primary" name="solve">Solve</button></center>
  29. </div>
  30. </form>
  31. </div>
  32. </div>
  33. </body>
  34. </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.  
  3. $random = rand(1, 9).rand(1, 9).rand(1, 9).rand(1, 9);
  4.  
  5. $_SESSION['captcha'] = $random;
  6.  
  7. $captcha = imagecreatefromjpeg("images/captcha.jpg");
  8. $color = imagecolorallocate($captcha, 0, 0, 0);
  9. $font = realpath('code.otf');
  10. imagettftext($captcha, 20, 0, rand(30, 180), rand(20, 70), $color, $font, $random );
  11. imagepng($captcha);
  12. imagedestroy($captcha);
  13. ?>

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

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.