crawl bot
Infoscript - Desenvolvimento web
infoscript_logo
PHP / MySQL
Captcha (Controle Humano)
Publicado por antlou em 19/07/2017

CAPTCHAs (Completely Automated Public Turing test to tell Computers and Humans Apart) são utilizados para impedir que softwares automatizados executem ações que degradam a qualidade do serviço de um sistema.
Vou desenvolver uma class simples em linguagem PHP que gera um captcha.
Passemos então ao código.

captcha_class.php

<?php
/*
 *****************************
 * Verificacao Captcha
 * By Antlou
 * 01/06/2013
 ******************************
*/

class captcha
{    
    function 
captcha()
    {
        
$this->criarImagem();
    }
    function 
criarImagem()
    {
        
//dimensoes da imagem captcha
    
$img imagecreate(120,25);    
        
// Cor de fundo do captcha
    
$fundo imagecolorallocate($img,221,221,221);
        
//Cor do texto captcha
    
$text imagecolorallocate($img,200,15,90);
    
imagefill($img,0,0,$fundo);
    
imagestring($img,5,25,5,$this->captchaString(),$text);
    
header("Content-Type: image/png");
    
imagepng($img);
    }
    function 
captchaString()
    {
        
//caracteres permitidos
    
$carateres='QWERTYUIPASDFGHJKLZXCVBNM
        23456789qwertyuipasdfghjkzxcvbnm'
;
        
//baralha caracteres e apresenta 8
    
$string substr(str_shuffle($carateres),0,8);
        
// Definir cookie do captcha
    
setcookie('captcha',$string);
        
//retorna string gerada
    
return $string;        
    }
}   
//fim da class

$c = new captcha;
?>

captcha.php

<style type="text/css">
    #titulo {
        width:300px;
        color: #C80F5A;    /*rgb(200,15,90);*/
        font-size:24px;
        font-weight:bold;
        margin-top:10px;
        margin-left:25px;
        margin-bottom:20px;
        }
      
    #captcha_form {
        margin: 50px auto;
        width: 350px;
        background: #FFFFCC;
        box-shadow:5px 5px 2px 2px #e1e1e1;
        border: 1px solid #C80F5A;     /*rgb(200,15,90);*/
        border-radius: 5px;    
            -moz-border-radius: 5px;
            -webkit-border-radius: 5px;
            -o-border-radius: 5px;
            -khtml-border-radius: 5px;    
        color: #555;    
        }
        
    label.captcha_label {
        margin-left: 30px;
        margin-bottom:15px;
        width: 100px;
        text-align: left;
        //border:1px solid #C00;
        }
        
     label.captcha_label_erro {
        color: #EE1C71;    /*#f8a3c6;*/
        font-size:18px;
        font-weight:bold;
        margin-left:30px;
        margin-bottom:15px;
        }
        
    img.captcha_label{
        margin-left: 30px;
        background:url(class.php);
        border:1px solid #AAAAAA;     /*#C80F5A;*/
        }
        
    input.captcha_input {
        margin-bottom:20px;
        margin-left: 30px;
        //margin-right:75px;
        width: 150px;
        color: #777;
        font-size: 18px;
        background-color:#DDDDDD;
        border:1px solid #AAAAAA; //#C80F5A; 
        }
        
    input.captcha_input:hover {
        background: #EEEEEE;
        }
    
    input.captcha_input_erro {
        margin-bottom:20px;
        margin-left: 30px;
        //margin-right:75px;
        width: 150px;
        color: #777;
        font-size: 20px;
        background-color:#f8a3c6;
        border:1px solid #C80F5A;     /*#AAAAAA;*/
        }
    
    input[type="submit"]{
        width: 200px;
        border:1px solid #C80F5A;    /*rgb(200,15,90);*/
        color:rgb(200,15,90);
        font-weight: bold;
        align:center;
        margin-bottom:20px;
        margin-left: 75px;
        margin-right:75px;
        padding-top:5px;
        padding-bottom:5px;
        border-radius:5px;
            -moz-border-radius:5px;
            -webkit-border-radius:5px;
            -o-border-radius:5px;
            -khtml-border-radius:5px;
            
    background: -webkit-linear-gradient(top, #FFFFCC, #C80F5A);
    background: -moz-linear-gradient(top, #FFFFCC, #C80F5A);
    background: -ms-linear-gradient(top, #FFFFCC, #C80F5A);
    background: -o-linear-gradient(top, #FFFFCC, #C80F5A);
    background: -khtml-linear-gradient(top, #FFFFCC, #C80F5A);   
    }
         
    input[type="submit"]:hover {
        background: #f8a3c6;    /*#EE1C71;*/
        box-shadow:0px 0px 2px 2px #999;    /*#e1e1e1;*/
        color:#555;
    }    

</style>
<?php
    
/*
    ********************************************
    * captcha                        
    * Auxiliar data to captcha               
    * Author:antlou                            
    * Version:1.0                              
    * Date:2017/07/08                          
    ********************************************
    */

global $PHP_SELF;

if(isset(
$_POST['submit']))
{
    
$a $_POST['txt'];
    
$b $_COOKIE['captcha'];
    
// verificar igualdade entre cookie e texto digitado
    
if($a==$b)    
    {
?>
    <form id="captcha_form">
    <div id="titulo">
        Verificação de Segurança
    </div>
    <h1>Texto Captcha Válido</h1>
    <h3>Voçê não é um robot!</h3>
    </form>

<?php
    
}
    else
    {
?>
<form id="captcha_form" 
    method="post" action="<?php $PHP_SELF?>">
<div id="titulo">Verificação de Segurança</div>
<label class="captcha_label">Voçê é um humano?</label><br>
<img class="captcha_label" src="class.php"><br />
<label class="captcha_label">
Introduza os novos caracteres v&iacute;siveis na caixa
</label>
<label class="captcha_label_erro">
Texto inválido
</label>
<input class="captcha_input_erro" type="text" name="txt"> 
<input class="captcha_submit" 
    type="submit" name="submit" value="VALIDAR">        
</form>
    
<?
    
}
}
else
{
?>
<form id="captcha_form" 
    method="post" action="<?php $PHP_SELF?>">
<div id="titulo">Verificação de Segurança</div>
<label class="captcha_label">
Voçê é um humano?</label>
<br>
<img class="captcha_label" src="class.php"><br />
<label class="captcha_label">
Introduza os carateres visiveis na caixa 
</label>
<input class="captcha_input" type="text" name="txt"> 
<input class="captcha_submit"
     type="submit" name="submit" value="VALIDAR">
</form>

<?php
}
?>


Resultado:

DEMO
Download Código
Espero ter sido útil. Deixe o grau de avaliação ao script.
1 votos - 5 pontos - 29 downloads
Média : 5,000

Infoscript - Desenvolvimento de websites, e-commerce e programação

Infoscript tem soluções para desenvolvimento web e programação para aproximar os seus clientes às necessidades atuais do mercado.
O design de produto, a criação de websites e lojas online entre outros, são os principais serviços que temos para lhe oferecer.
Contate-nos através do endereço geral@infoscript.pt
Nós teremos uma solução para si à medida.