PHP - Simple Captcha Generator

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

In this tutorial we will create a Simple Captcha Generator using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.

Before we 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 has been used for the layout 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 you 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 - Simple Captcha Generator</h3>
  18. <hr style="border-top:1px dotted #ccc;"/>
  19. <form action="" method="POST">
  20. <img src="captcha.php" />
  21. <br />
  22. <br />
  23. <input type="text" name="captcha" />
  24. <?php
  25. if(ISSET($_POST['verify'])){
  26. if(!empty($_POST['captcha'])){
  27. if($_SESSION['captcha'] == $_POST['captcha']){
  28. echo "<label class='text-success'>Validated</label>";
  29. }else{
  30. echo "<label class='text-danger'>Invalid captcha!</label>";
  31. }
  32. }else{
  33. echo "<label class='text-warning'>Please fill up the required field!</label>";
  34. }
  35. }
  36. ?>
  37. <br /><br />
  38. <button name="verify" class="btn btn-primary">Verify</button>
  39.  
  40. </form>
  41. </div>
  42. </body>
  43. </html>

Creating the Main Function

This code contains a main function for the application. This code will render a captcha image when load, and the user will try to verify the image to get access. To do that write this codes inside the text editor and save it as captcha.php .

  1. <?php
  2.  
  3. $random = 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 = 'code.otf';
  10. imagettftext($captcha, 20, 0, rand(30, 200), rand(20, 70), $color, $font, $random );
  11. imagepng($captcha);
  12. imagedestroy($captcha);
  13. ?>

There you have it we successfully created a Simple Captcha Generator 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.