magkopian/php-math-captcha

一个用于生成和验证数学验证码图像的简单类

1.0.0 2014-10-04 14:13 UTC

This package is not auto-updated.

Last update: 2024-09-28 17:12:29 UTC


README

这是一个PHP类,用于生成包含简单数学问题的图像(数学验证码),以保护您网站的表单免受垃圾邮件机器人攻击。

如何安装

您可以通过使用composer非常容易地安装此类。您只需运行

composer require magkopian/php-math-captcha:1.0.*

或者在您的 composer.json 文件中添加它作为依赖项

{
	"require": {
		"magkopian/php-math-captcha": "1.0.*"
	}
}

然后运行

composer update

此外,别忘了将composer的 autoload.php 文件包含到您的代码中。

如何使用

要生成验证码,您只需

session_start();

$mathCaptcha = new MathCaptcha\MathCaptcha();

$mathCaptcha->generate();
$mathCaptcha->output();

MathCaptcha 类使用会话变量,因此您必须在实例化 MathCaptcha 对象之前调用 session_start() 函数。

您可以选择向 MathCaptcha 类的构造函数提供一个验证码标识符,如果您想在您的网站上使用多个验证码。

要验证用户的答案,您只需

session_start();

$mathCaptcha = new MathCaptcha\MathCaptcha();

if ( $mathCaptcha->check($captcha_answer) === true ) {
	// Correct answer
}
else {
	// Incorrect answer
}

如果您在您的网站上使用多个验证码,您还需要向 MathCaptcha 类的构造函数提供验证码的标识符。

查看 test_form.phpmath_captcha.php 文件以获取一个工作示例。

要求

PHP 5,GD 2.0.1或更高版本(建议使用2.0.28或更高版本)