In this post I will show you how you can create your own antispam system, making a CAPTCHA in the PHP programming language. ENTERS!
WE START:
I come again with another tutorial for programmers, and today we are going to learn how to create a CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart, for its acronym in English), and it is an antispam system to prevent SPAM or prevent the access to robots of the access to a certain place or execution of a certain action, so if you need a CAPTCHA, you are in the right place.
Explanation of this file:
This system is divided into 3 files:
- index.php (contains a test form).
- captcha.php (generates an image that has a random text).
- validar.php (gets what to do if the captcha is correct or not).
IF YOU DO NOT WANT to copy the codeAt the bottom of this post I leave you a download link to the source files of the code.
Index.php file:
<!DOCTYPE html><!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title>Learning something with Master Coria</title> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(function(){ $("btn_recargar").click(function(){ document.location.reload(); return false; }); $("#btn_iniciar").click(function(e){ e.preventDefault(); var data = $('#frm').serialize(); $.ajax({ url: "validar.php", type: "post", data: data, success: function(res){ alert(res); } }); }); }); </script> </head> <body> <form id="frm" action=""> <table> <tr> <td>User: </td><td><input type="text" name="user" /></td> </tr> <tr> <td>Password: </td><td><input type="text" name="pass" /></td> </tr> <tr> <td><img src="captcha.php" /></td><td><input type="text" name="captcha" /></td><td><button id="recargar">Recharge</button></td> </tr> <tr> <td><input type="submit" id="btn_iniciar" value="Login" /></td> </tr> </table> <input type="hidden" name="trp-form-language" value="en"/></form> </body></html>
Captcha.php file:
<?php header("content-type: imagen/png"); $imagen = imagecreate(90, 50); $color_fondo = imagecolorallocate($imagen, 59, 66, 255); $color_texto = imagecolorallocate($imagen, 255, 255, 255); function generar_caracteres($chars, $length){ $captcha = null; for($i=0;$i
Now this is the validar.php file:
If you want to understand the total operation of this system, I leave you the 2 video tutorials of the youtuber «PuroCode» (I also leave you the source code in case you want to have it ordered at the bottom of the page):
#1:
#2:
Download CAPTCHA system in PHP:
to downloadDownload source code
You can see our most recent posts on our page start from the blog. Like us on our Facebook page so you don't miss any news. GOOD MORNING AND GREETINGS FROM OUR TEAM!