How to Create Captcha in PHP

In this article, we are going to create Captcha using PHP. This simple article will help you on how to create this simple captcha with this simple source code. Enable you to generate a random code and you can use this to protect your page from random spammers. You can use this also as a type of a simple test that the response is generated by a human being and it's a very common on other websites. Creating random code, TextBox, and one button as shown in the image below. ResultHere's the short source code for the image above.
  1. <form class="form-horizontal" method="POST">
  2.  
  3. <img src="generatecaptcha.php?rand=<?php echo rand(); ?>" id='image_captcha' >
  4. <a href='javascript: refreshing_Captcha();'><i class="icon-refresh icon-large"></i></a>
  5. <script language='JavaScript' type='text/javascript'>
  6. function refreshing_Captcha()
  7. {
  8. var img = document.images['image_captcha'];
  9. img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
  10. }
  11. </script>
  12.  
  13. <div class="control-group">
  14. <label class="control-label" for="inputPassword">Enter the Code Above</label>
  15. <div class="controls">
  16. <input id="code" name="code" type="text" placeholder="Enter the Code Above" required></td>
  17. </div>
  18. </div>
  19.  
  20. <div class="control-group">
  21. <div class="controls">
  22. <button type="submit" name="submit" class="btn btn-primary"><i class="icon-ok icon-large"></i> Submit</button>
  23. </div>
  24. </div>
  25.  
  26. </form>
Here's the source code for generating a random code.
  1. <img src="generatecaptcha.php?rand=<?php echo rand(); ?>" id='image_captcha' >
  2. <a href='javascript: refreshing_Captcha();'><i class="icon-refresh icon-large"></i></a>
  3. <script language='JavaScript' type='text/javascript'>
  4. function refreshing_Captcha()
  5. {
  6. var img = document.images['image_captcha'];
  7. img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
  8. }
  9. </script>
In the image below, if the user matches random code. ResultIn the image below, if the user does not match the random code. ResultHope that this article will help you a lot. Share us your thoughts and comments below. Thank you so much for dropping by and reading this tutorial post. For more updates, don’t hesitate and feel free to visit this website more often and please share this with your friends or email me at [email protected]. Practice Coding. Thank you very much.

Add new comment