SilentCaptcha usage Examples

The following examples all do mainly the same thing as a way of demonstrating how easy SilentCaptcha is you use and the various different settings are applied.

Form without SilentCaptcha

<html>   
 
  <?php
    if(isset($_POST['submit']))
    {
      $headers = 'From: '.$_POST['email_address'].'' . "\r\n" .
        'Reply-To: '.$_POST['email_address'].'' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();       
      mail('email@example.com', 'test', $_POST['comment'], $headers);
    }
  ?>
  
  <form action="" method="POST">
    Email:<input type="text" name="email_address" value=""><br>
    Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>   
    <input type="submit" name="submit" value="Submit">
  </form>
  
</html>

The above checks that a form has been submitted and emails email@example.com. The body of the email is the submitted comment and the reply address is the submitted user email address.

Simple Example

<html>   
 
  <?php 
    // Call SilentCaptcha code
    include 'silentcaptcha/silentcaptcha.php';

    // Get SilentCaptcha result, replaces form submission check
    if(silent_captcha_successful())
    {
      $headers = 'From: '.$_POST['email_address'].'' . "\r\n" .
        'Reply-To: '.$_POST['email_address'].'' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();        
      mail('email@example.com', 'test', $_POST['comment'], $headers);
    }
  ?>
  
  <form action="" method="POST">
    Email:<input type="text" name="email_address" value=""><br>
    Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>
    <?php 
        // Apply Captcha to your form 
        silent_captcha();
    ?>      
    <input type="submit" name="submit" value="Submit">
  </form>
  
</html>

The above example assumes that all of SilentCaptcha’s defaults are used. Note that you no longer need to check that the form was submitted.

This form will now be checked for non human use.

Changing Default Settings

<html>   
 
  <?php 
    // Call SilentCaptcha code
    include 'silentcaptcha/silentcaptcha.php';

    // Apply Settings
    $silent = array(
      'timestamp_max_minutes' => 120,
      'test_email_not_disposable' => false,
      'test_godwins_law' => true
    );

    // Get SilentCaptcha result, replaces form submission check
    if(silent_captcha_successful())
    {
      $headers = 'From: '.$_POST['email_address'].'' . "\r\n" .
        'Reply-To: '.$_POST['email_address'].'' . "\r\n" .
        'X-Mailer: PHP/' . phpversion();       
      mail('email@example.com', 'test', $_POST['comment'], $headers);
    }
  ?>
  
  <form action="" method="POST">
    Email:<input type="text" name="email_address" value=""><br>
    Comment: <textarea name="comment" rows="5" cols="40"></textarea><br>
    <?php 
        // Apply Captcha to your form 
        silent_captcha();
    ?>      
    <input type="submit" name="submit" value="Submit">
  </form>
  
</html>

Processing in Another File

Error Feedback

Advance Error Feedback

Two forms on the same page

<?php

    // Call SilentCaptcha
    include 'silentcaptcha/silentcaptcha.php';

    // Get SilentCaptcha result either;
    if(silentcaptcha_result())
    {
        // your form processing code goes here...
        // in this example we'll write 'Form OK' on the screen
        echo "<p>Form OK</p>";
    }
?>
Was this article helpful to you? Yes No

How can we help?