
En este post te enseñaré cómo es que puedes crear tu propio sistema antispam, haciendo un CAPTCHA en el lenguaje de programación PHP. ¡ENTRA!
INICIAMOS:
Vengo de nuevo con otro tutorial para los programadores, y hoy vamos a aprender a crear un CAPTCHA (Completely Automated Public Turing test to tell Computers and Humans Apart, por sus siglas en inglés), y es un sistema antispam para prevenir SPAM o impedir el acceso a robots del acceso a cierto lugar o ejecución de una cierta acción, así que si necesitas un CAPTCHA, estás en el lugar correcto.
Explicación de este archivo:
Este sistema se divide en 3 archivos:
- index.php (contiene un formulario de prubea).
- captcha.php (genera una imagen que tiene un texto aleatorio).
- validar.php (obtiene lo que se va a hacer si el captcha es correcto o no).
SI NO QUIERES copiar el código, hasta abajo de este post te dejo un link de descarga a los archivos fuente del código.
Archivo index.php:
<!DOCTYPE html><!DOCTYPE html><html> <head> <meta charset="utf-8" /> <title>Aprendiendo algo con 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"> <table> <tr> <td>Usuario: </td><td><input type="text" name="user" /></td> </tr> <tr> <td>Contraseña: </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">Recargar</button></td> </tr> <tr> <td><input type="submit" id="btn_iniciar" value="Login" /></td> </tr> </table> </form> </body></html>Archivo captcha.php:
<?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<$length;$i++){ $rand = rand(0, count($chars)-1); $captcha .= $chars[$rand]; } return $captcha; } $captcha = generar_caracteres(array(0,1,2,3,4,5,6,7,8,9,'a','b','c','d','e','f','h','x','z'), 4); setcookie('captcha', sha1($captcha), time()+60*3); imagettftext($imagen, 20, 10, 15, 38.5, $color_texto, "arial.ttf", $captcha); imagepng($imagen); ?>Ahora, este es el archivo validar.php:
<?php if(isset($_POST)){ $captcha_cookie = $_COOKIE['captcha']; $captcha_user = $_POST['captcha']; if($captcha_cookie == sha1($captcha_user)){ echo "Correcta."; setcookie("captcha", "", time()-3600); /* = CORRECTA = TU CONTENIDO A HACER ... */ } else { /* = INCORRECTA = TU CONTENIDO A HACER ... */ echo "Incorrecta."; } } ?>Si quieres entender el funcionamiento total de este sistema, te dejo los 2 video tutoriales del youtuber «PuroCódigo» (también te dejo el código fuente por si lo quieres tener ordenado en la parte inferior de la página):
#1:
#2:
Descargar sistema de CAPTCHA en PHP:
DescargarDescargar código fuente
Nota: Los enlaces conducen directamente a su descarga.
Puedes ver nuestros posts más recientes en nuestra página inicio del blog. Danos like en nuestra página de Facebook para que no te pierdas ninguna novedad. ¡BUEN DÍA Y SALUDOS POR PARTE DE NUESTRO EQUIPO!