Geração de códigos captcha em formulários HTML com PHP e PostgreSQL

Publicado por Cláudio Souza Jr. (última atualização em 06/01/2015)

[ Hits: 4.513 ]

Download classe.php




Requerimentos:

- PostgreSQL 9.2
- Apache 2
- PHP 5 (5.4.35)
- NTP (facultativo)
- Extensões PHP: php5-gd e php5-pgsql
- A opção global "date.timezone" do "php.ini" foi configurada
- O navegador tem que suportar JavaScript.

Segue anexada a classe e os códigos dos demais arquivos.

Obs.: o path aqui no meu servidor é: "http://localhost/testes/php/classes_php/captcha_simples". Ao mudar isso, atente para o modo "3" do método "CriaCaptcha", em "classe.php".

Críticas construtivas, sugestões e análises são bem-vindas.

  



Esconder código-fonte

::: Código do arquivo index.php :::

<?php
include 'classe.php';
$Captcha = new Captcha();
$Captcha->Definicao(); ?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
        <title>Teste Captcha Simples</title>
        <link rel='icon' type='image/png' href=''>
        <link rel='stylesheet' href='style.css' type='text/css'>
    </head>
    <body onload="<?=$Captcha->body_onload?>" onunload="<?=$Captcha->body_onunload?>">
        <div class="corpo">
        <?php
        if($Captcha->passo == '1') {
            $Captcha->FormCaptcha($Captcha->passo, $Captcha->captcha_id, $Captcha->captcha_cod);
        }
        elseif($Captcha->passo == '2') {
            $Captcha->CriaCaptcha('1', $Captcha->captcha_id, $Captcha->captcha_cod);
        }?>
        </div>
        <script type="text/javascript" src="javascript.js"></script>
    </body>
</html>

--------------------------------------------------------------------------------------
::: Código do arquivo captcha.php :::

<?php
header("Content-type: image/png");
include_once 'classe.php';
$captcha = new Captcha();
$captcha->CriaImgCaptcha($_SESSION['captcha_cod']);
?>

--------------------------------------------------------------------------------------
::: Código do arquivo style.css:::

.corpo {
    margin-left: 120px;
    margin-right: 120px;
    margin-top: 120px;
    padding-left: 120px;
    padding-right: 120px;
}
.input-text {
    border: solid 1px;
    border-color: #2f96b4;
    padding: 2px;
    text-transform: uppercase;
}

--------------------------------------------------------------------------------------
::: Código do arquivo javascript.js:::

/**
 * Script de validacao e funcoes genericas
 */
function RefreshThisPage(){
    window.location = window.location;
}
function SetFormData(){
    sessionStorage["captcha_cod"] = document.FormCaptcha.captcha_cod.value;
    sessionStorage["form_texto"] = document.FormCaptcha.form_texto.value;
}
function LoadFormData(){
    var captcha_cod = sessionStorage.captcha_cod;
    var form_texto = sessionStorage.form_texto;
    if (captcha_cod != undefined ||form_texto != undefined){
        document.FormCaptcha.captcha_cod.value = captcha_cod;
        document.FormCaptcha.form_texto.value = form_texto;
    }
}
function UnsetFormData(){
    sessionStorage.removeItem('captcha_cod');
    sessionStorage.removeItem('form_texto');
}
function ValidaForm(){
    if(document.FormCaptcha.form_texto.value === ""){
        alert('Informe o texto!');
        document.getElementById('form_texto').focus();
        return false;
    }
    if(document.FormCaptcha.captcha_cod.value === ""){
        alert('Informe o codigo captcha!');
        document.getElementById('captcha_cod').focus();
        return false;
    }
    if(document.FormCaptcha.captcha_cod.value.length < '6'){
        alert('O codigo captcha possui 6 digitos!');
        document.getElementById('captcha_cod').focus();
        return false;
    }
    return true;
}

--------------------------------------------------------------------------------------
::: Tabela do banco de dados :::

DROP TABLE IF EXISTS tb_captcha CASCADE;

CREATE TABLE tb_captcha
(
  captcha_id integer NOT NULL, -- Id do registro dos dados do sistema
  captcha_cod character varying, -- Código captcha
  captcha_data date, -- Data de alteração
  CONSTRAINT captcha_id PRIMARY KEY (captcha_id)
)
WITH (OIDS=FALSE);

COMMENT ON TABLE tb_captcha IS 'Dados do sistema, informações gerais';
COMMENT ON COLUMN tb_captcha.captcha_id IS 'Id do registro dos dados do sistema';
COMMENT ON COLUMN tb_captcha.captcha_cod IS 'Código captcha';
COMMENT ON COLUMN tb_captcha.captcha_data IS 'Data de alteração';

INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (1, '0880A2', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (2, 'A7D3E4', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (3, '765C4B', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (4, '2B7C80', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (5, '323FD0', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (6, '967F69', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (7, 'A79653', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (8, 'FE42A3', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (9, 'D2A67C', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (10, 'C27EC0', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (11, '304C6B', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (12, '823F10', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (13, 'DA599E', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (14, '1199D9', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (15, 'DDE76B', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (16, '4D7BA5', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (17, '749E4E', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (18, 'DBB6E9', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (19, '69380A', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (20, 'D5A082', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (21, '4403DC', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (22, '73570A', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (23, '61E5B0', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (24, '6FCE3C', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (25, '8AC22D', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (26, 'C4DBA5', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (27, '47DBD6', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (28, 'C50453', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (29, '484B1F', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (30, 'DEB59D', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (31, 'DC3EA6', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (32, 'BA46E1', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (33, 'A335E1', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (34, 'F39EF0', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (35, '3923B9', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (36, '2D794C', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (37, '991AA2', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (38, 'F4F648', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (39, 'AE5E8F', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (40, '811C71', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (41, '002C60', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (42, '7D7CC3', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (43, '3A480A', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (44, '979E0F', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (45, '5D7379', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (46, '2D7E9D', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (47, '075FB7', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (48, 'C3F1D7', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (49, 'CC9C8A', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (50, '24086C', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (51, 'C8CDC8', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (52, '072CCE', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (53, '285DE3', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (54, '0B9586', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (55, 'AC47E0', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (56, '59B8BD', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (57, '4CE688', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (58, 'CBABA3', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (59, '363435', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (60, '55717A', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (61, '60199D', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (62, 'BAF01B', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (63, 'AFE56E', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (64, '11E096', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (65, '717695', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (66, 'F01062', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (67, '18DC86', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (68, '63420D', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (69, '94F9F5', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (70, '04893A', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (71, '02F6C3', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (72, '2B906C', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (73, 'E553B6', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (74, '8ECF62', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (75, 'C77C40', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (76, 'B2C017', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (77, '10C9E4', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (78, '0FCE1B', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (79, 'E7A71E', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (80, '18FD78', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (81, '77C4F3', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (82, '3CEA7C', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (83, '9D5611', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (84, 'BD7D5F', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (85, 'D2A9E3', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (86, 'FDF115', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (87, '12C1FB', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (88, '0BBFCF', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (89, 'F45DBE', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (90, '5A2EE5', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (91, '62CA57', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (92, '1B6F5B', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (93, '8B2C7D', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (94, '1CD166', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (95, '130DEF', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (96, '91ED08', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (97, 'C3807D', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (98, 'E3599F', '2014-12-31');
INSERT INTO tb_captcha (captcha_id, captcha_cod, captcha_data) VALUES (99, 'A9BFBB', '2014-12-31');

Scripts recomendados

HTML BASICO PARA INTERNET

Script para Integração com a Receita Federal

guarda num arquivo txt o host, ip, data e hora de acesso do visitante

PARTE 1- WEBSERVICE EM PHP

WAM Sistema de Notícias


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts