secure captcha logo
Important: domain name change. We are moving from web-utils.eu to securecaptcha.net. If you use our service, change host name in your code.

Secure CAPTCHA - installation in PHP environment

This page contains information about integrating the Secure CAPTCHA service in the PHP environment.

Overview

We assume, that you have already obtained your siteID and privateKey. If not, check instructions at the installation page.

To integrate Secure CAPTCHA with your site, you will have to complete following steps:
 1. Download a script
 2. Integrate it with your form

1. Download a script

You need a handwrittenCaptcha.php script. Download it, save it and copy to your PHP project location, so that it can be found with the require statement. To test if it is installed properly run this code in your web environment.
    require 'handwrittenCaptcha.php';
  
If you can run this code and there are no errors, it means, that script has been installed successfully.

2. Integrate with your form

Script handwrittenCaptcha provides CaptchaService class with constructor: CaptchaService($siteID, $privateKey, $proxyServer=null, $proxyPort=null). You can obtain siteID and privateKey after creating the site definition at our admin panel. If your server is behind the proxy, you may need to use proxyHost and proxyPort parameters.

CaptchaService class provides following methods.
You can see an example below.
#!/usr/local/bin/php
<?PHP
    session_start();
?>
<html><body>
<?PHP
    // we import the script
    require 'handwrittenCaptcha.php';
    // replace it with your siteID
    $siteID = 'put your siteID here!';
    // replace it with your privateKey 
    $privateKey = 'put your privateKey here!';

    // initialization
    $captchaService = new CaptchaService($siteID, $privateKey);

    // if the answer was submitted
    if (isset($_REQUEST['formSubmitted'])) {
	$answer = $_REQUEST['answer'];
        // restore data
        $value = $_SESSION['captchaData'];
        $hash = $value['hash'];
        $mac = $value['mac'];
        $timestamp = $value['timestamp'];

        unset($_SESSION['captchaData']);

        // validate offline
        $okOffline = $captchaService->validateOffline($hash, $mac, $timestamp, $answer);
        if ($okOffline) print 'Offline validation: OK. ';

        // validate online
        $okOnline = $captchaService->validateOnline($hash, $timestamp, $answer, $error);
        if ($okOnline) print 'Online validation: OK. ';
        if ($error != null) print 'Online validation error: ' . error;
    }
?>
<form action="" method="post">
<?PHP
    // create new captcha
    if ($captchaService->getCaptcha($value)) {
        $hash = $value['hash'];
        $mac = $value['mac'];
        $timestamp = $value['timestamp'];
        $fragment = $value['fragment'];
        // store captcha values for later use	
        $_SESSION['captchaData'] = array('hash' => $hash, 'mac' => $mac, 'timestamp' => $timestamp);
        // print captcha html/xhtml fragment
        echo $fragment;
    } else {
        echo 'An error occurred: ' . $value;
    }
?>
  Rewrite text from the image above (only small Latin letters):
  <input type="text" name="answer" value="" />
  <input type="submit" name="formSubmitted" value="Submit" />
</form>
</body>
</html>
How this example works: