以gregwar/captcha包为例:
https://packagist.org/packages/gregwar/captcha
第一步:
下载安装:composer require gregwar/captcha
第二步:
在master.php类文件中引入
require_once __DIR__.'/../../vendor/autoload.php';
use Gregwar\Captcha\CaptchaBuilder;
在app类中定义属性,并在构造方法中把captcha对象赋值给该属性
public $yzm;
$this->yzm = new CaptchaBuilder;
第三步:
测试地址:http://127.0.0.1/index.php?test-master-test1-yzm_yz&yzm=stwzn
显示验证码和验证验证码
模板文件中:
xxxxxxx
111111111111111111111
class action extends app
{
public function display()
{
$action = $this->ev->url(3);
if (!method_exists($this, $action))
$action = "index";
$this->$action();
exit;
}
private function index()
{
//这两步必须的,否则会不显示验证码
$this->yzm->build();
$_SESSION['phrase'] = $this->yzm->getPhrase(); //保存进session
$yzm_img_url = $this->yzm->inline();
$this->tpl->assign('yzm', $yzm_img_url);
$this->tpl->display('test1');
}
//验证码验证码测试
private function yzm_yz()
{
print_r($_SESSION);
$userInput = $this->ev->get('yzm');
//echo "
";
//echo $userInput . '-----';
// print_r($this->ev->get('yzm'));
// var_dump($this->yzm->testPhrase($userInput));
//这里这样验证始终有些问题,不建议
//https://www.jianshu.com/p/8e4ac7852b5a
/* if ($this->yzm->testPhrase($userInput)) {
// instructions if user phrase is good
echo 'ok';
} else {
// user phrase is wrong
echo 'no';
}*/
// 验证验证码
if ($userInput === $_SESSION['phrase']) {
echo 'ok';
} else {
echo 'no';
}
}
}