1.导入扩展包到 ThinkPHP/Extends/*
2.创建一个Public 模块的生成验证码的 code 方法
<?php
class PublicAction extends Action{
public function code(){
$w=isset($_GET['w'])?
$_GET['w']:30
;
$h=
isset($_GET['h'])?
$_GET['h']:30
;
import('ORG.Util.Image');
Image::buildImageVerify(2,1,'png',$w,$h,'code');//长度,类型,图片格式,宽,高,session 中的名字
}
}
?>
3.页面上运用
验证码:<input type="text" name="code" />
<img src="__APP__/Public/code" οnclick="this.src=this.src+'?'+Math.random" />
4.后台获取验证码
$code=$_POST['code'];
if($_SESSION['verify'] !== md5($code)){
$this->error("验证码错误!");
}