PHP Classes

File: test_recaptcha_input.php

Recommend this page to a friend!
  Packages of Manuel Lemos   PHP Forms Class with HTML Generator and JavaScript Validation   test_recaptcha_input.php   Download  
File: test_recaptcha_input.php
Role: Example script
Content type: text/plain
Description: Example to demonstrate how to use the reCAPTCHA custom input
Class: PHP Forms Class with HTML Generator and JavaScript Validation
HTML forms generation and validation.
Author: By
Last change: Added support to associate a label to the CAPTCHA input.
Removed test code.
Made the RECAPTCHA custom input support Recaptcha API version 2.
Date: 4 years ago
Size: 2,263 bytes
 

 

Contents

Class file image Download
<?php
/*
 *
 * @(#) $Id: test_recaptcha_input.php,v 1.5 2022/03/06 11:28:20 mlemos Exp $
 *
 */

    require('forms.php');
    require('http.php');
    require('form_recaptcha.php');

    $key = ''; $line = __LINE__;
    $private_key = '';
    if(strlen($key) == 0)
        die('Please go to the Recaptcha site '.
        'https://www.google.com/recaptcha/admin/create to obtain the public '.
        'and private keys to set in the line '.$line.'.');
    $form = new form_class;
    $form->NAME = 'captcha_form';
    $form->METHOD = 'GET';
    $form->ACTION = '';
    $form->debug = 'error_log';
    $error = $form->AddInput(array(
        'TYPE'=>'custom',
        'ID'=>'captcha',
        'LABEL'=>'<u>C</u>lick in the "I\'m not a robot" check box to show you are nice person:',
        'ACCESSKEY'=>'E',
        'CustomClass'=>'form_recaptcha_class',
        'Key'=>$key,
        'PrivateKey'=>$private_key,
        // 'ValidationErrorMessage'=>'The validation did not pass.',
    ));
    if(strlen($error))
    {
        die('Error: '.$error);
    }
    $form->AddInput(array(
        'TYPE'=>'submit',
        'VALUE'=>'Submit',
        'NAME'=>'doit'
    ));

    /*
     * Always check if LoadInputValues returns any errors to detect any
     * configuration or reCAPTCHA access problem
     */
    if(strlen($error = $form->LoadInputValues($form->WasSubmitted('doit'))))
        die('Error processing reCAPTCHA response: '.$error);

    $verify = array();
    if($form->WasSubmitted('doit'))
    {
        if(($error_message = $form->Validate($verify))=='')
            $doit = 1;
        else
        {
            $doit = 0;
            $error_message = HtmlSpecialChars($error_message);
        }
    }
    else
    {
        $error_message = '';
        $doit = 0;
    }

    $onload = HtmlSpecialChars($form->PageLoad());

?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Test for Manuel Lemos' PHP form class using the reCAPTCHA plug-in input</title>
</head>
<body onload="<?php    echo $onload; ?>" bgcolor="#cccccc">
<center><h1>Test for Manuel Lemos' PHP form class using the reCAPTCHA plug-in input</h1></center>
<hr />
<?php
  if($doit)
    {
?>
<center><h2>The validation passed.</h2></center>
<?php
    }
    else
    {
        $form->StartLayoutCapture();
        $title = 'Form CAPTCHA plug-in test';
        $body_template = 'form_recaptcha_body.html.php';
        include('templates/form_frame.html.php');
        $form->EndLayoutCapture();
        $form->DisplayOutput();
    }
?>
<hr />
</body>
</html>