PHP Class


Download RainCaptcha-1.1.0.php

Initializing

To start using RainCaptcha, include the RainCaptcha-1.1.0.php file and create a new instance of the RainCaptcha class:

require_once('path/to/raincaptcha/class/RainCaptcha-1.1.0.php');
$rainCaptcha = new \RainCaptcha();

Getting the image

To get the URL of the CAPTCHA image, use the getImage() method of the RainCaptcha class:

$rainCaptcha->getImage();

Example: Displaying the image on a html page

If you want to display the CAPTHCA image on a web page, we recommend you to use an img tag:

<?php
	$rainCaptcha = new \RainCaptcha();
?>
<img src="<?php echo $rainCaptcha->getImage(); ?>" />

Example: Editing the image on server side

You may want to edit the image on your server before returning it to the user. First, load image from RainCaptcha:

$rainCaptcha = new \RainCaptcha();
$captchaImageGd = imageCreateFromPNG($rainCaptcha->getImage());

Then, you can edit your image. For example, you can apply a colorize filter to it:

imageFilter($captchaImageGd, IMG_FILTER_COLORIZE, 240, 0, 0);

After you modified the image, you can return it to the user:

header('Content-type: image/png');
imagePNG($captchaImageGd);

Checking the answer

To check whether the user's response is correct or not, use the checkAnswer($answer) method of the RainCaptcha class:

$rainCaptcha->checkAnswer($captchaAnswer)

Where $captchaAnswer is a string with the submitted answer. If the submitted answer is correct, this method will return true, otherwise it will return false. Call this method only once; if you need to use the returned value more than once, save it in a variable.

Example

if(!isset($_GET['captcha']) || empty($_GET['captcha']))
	echo 'You did not enter the CAPTCHA!';
else {
	$rainCaptcha = new \RainCaptcha();
	if($rainCaptcha->checkAnswer($_GET['captcha']))
 		echo 'You have passed the CAPTCHA test!';
	else
		echo 'You have not passed the CAPTCHA test!';
}

Advertisement