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 yoursiteID 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 therequire statement.
To test if it is installed properly run this code in your web environment.
require 'handwrittenCaptcha.php';
2. Integrate with your form
ScripthandwrittenCaptcha 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. getCaptcha(&$ret)- requests for a new CAPTCHA and returnsTrueon success andFalseon error. Return parameter$retis set to array withhash,mac,timestampandfragmentvalues on success or to error code on failure.HtmlFragmentis a HTML/XHTML fragment you should print directly into your page.Hash,macandtimestampare used to validate the answer and you should keep them somewhere (ie. in user's session object) and restore them when user submits the form. You should not reveal them to the user.validateOffline($hash, $mac, $timestamp, $answer)validates user's answer using offline algorithm (hmac). It returnsTrueif answer is valid orFalseotherwise.$hsh,$macand$timestampparameters are values returned bygetCaptchamethod;$answeris an answer provided by the user. If you are not sure, that you use the same values that has been generated ingetCaptchamethod, use online validation.validateOnline($hash, $timestamp, $answer, &$error)validates user's answer using online algorithm (http request). It returnsTrueif answer is valid and no errors occurred orFalseotherwise. Return parameter$erroris set to error code on validation error or tonullotherwise.$hshand$timestampare values returned bygetCaptchamethod;$answeris an answer provided by the user.
#!/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>
- at line 15:
CaptchaServiceis initialized, - at line 41: new CAPTCHA is created,
- at line 47: CAPTCHA values are saved for later use,
- at line 49: CAPTCHA fragment is printed,
- at line 21: CAPTCHA values are restored,
- at lines 29 and 33: CAPTCHA answer is validated.
