thinkphp原装的验证码感觉不太好看,用phpcms的时候,看到他们的验证码挺不错的,拿过来用一下。
主要代码如下:
config.php中<?php <br />
return array(<br>
//'配置项'=>'配置值'<br>
/**<br>
* Verify常量设置<br>
*/<br>
//'VERIFY_WIDTH' => '130',<br>
//验证码的宽度<br>
'VERIFY_WIDTH' => 130,<br>
<br>
//验证码的高<br>
'VERIFY_HEIGHT' => 50,<br>
<br>
//设置随机生成因子<br>
'VERIFY_CHARSET' => 'abcdefghkmnprstuvwyzABCDEFGHKLMNPRSTUVWYZ23456789',<br>
<br>
//设置背景色<br>
'VERIFY_BACKGROUND' => '#EDF7FF',<br>
<br>
//生成验证码字符数<br>
'VERIFY_CODE_LEN' => 4,<br>
<br>
//字体大小<br>
'VERIFY_FONT_SIZE' => 20,<br>
<br>
);<br>
?>
IndexAction.class.php方法中的代码如下<?php <br />
// 本类由系统自动生成,仅供测试用途<br>
class IndexAction extends Action {<br>
public function index(){<br>
$this->display();<br>
}<br>
<br>
public function verify(){<br>
import('@.Class.Verify.Verify');<br>
$verify = new Verify();<br>
<br>
$code_len = C('VERIFY_CODE_LEN');<br>
$font_size = C('VERIFY_FONT_SIZE');<br>
$width = C('VERIFY_WIDTH');<br>
$height = C('VERIFY_HEIGHT');<br>
$font_size = C('VERIFY_FONT_SIZE');<br>
$font_size = C('VERIFY_FONT_SIZE');<br>
$font_size = C('VERIFY_FONT_SIZE');<br>
<br>
//echo $code_len;die;<br>
if (isset($code_len) && intval($code_len)) $verify->code_len = intval($code_len);<br>
if ($verify->code_len > 8 || $verify->code_len
$verify->code_len = 4;<br>
}<br>
if (isset($font_size) && intval($font_size)) $verify->font_size = intval($font_size);<br>
if (isset($width) && intval($width)) $verify->width = intval($width);<br>
if ($verify->width
$verify->width = 130;<br>
}<br>
<br>
if (isset($height) && intval($height)) $verify->height = intval($height);<br>
if ($verify->height
$verify->height = 50;<br>
}<br>
$max_width = $verify->code_len * 28;<br>
$max_height = $verify->font_size * 2;<br>
if($verify->width > $max_width) $verify->width = $max_width;<br>
if($verify->height > $max_height) $verify->height = $max_height;<br>
$verify->doimage();<br>
session('code',$verify->get_code());<br>
<br>
}<br>
<br>
public function check(){<br>
$verify = strtolower($_GET['verify']);<br>
$code = $_SESSION['code'];<br>
if ($verify == $code){<br>
$this->success('你太厉害了,竟然把验证码写对了!',U('Index/index'));<br>
}else{<br>
$this->error('你个大笨蛋,竟然连验证码都写不对,还能干啥!');<br>
}<br>
}<br>
}
自从用了thinkphp高级版本出现php环境不支持后,一直在使用thinkphp 3.1.3,所以我的代码是基于这个版本的。如果在其他版本上有需求,请自行修改,全部代码会在附件中分享。
在thinkphp中使用phpcms的验证码.zip ( 1.57 MB 下载:28 次 )
AD:真正免费,域名+虚机+企业邮箱=0元