返回 全球(切换) 

如何做thinkPHP的验证码

2013/12/27 16:09:41166
详细描述


Thinkphp中使用验证码很容易,只要调用thinkphp现有的方法就可以。当然,php的GD库肯定是要开的(就是在php.ini中要加载gd模块),其次要看你的ThinkPHP\Lib\ORG\Util\下面是否有Image.class.php类,没有的话也是没有办法显示验证码的。

首先,在写Action文件,如:IndexAction.class.php.
1.<?php 
2.class IndexAction extendsAction{ 
3.   //显示验证码 
4.    publicfunction verifyTest() { 
5.     $this->display(); 
6.   } 
7.     
8.   //检验验证码是否正确 
9.    publicfunction verifyCheck() { 
10.     //防止页面乱码 
11.         header('Content-type:text/html;charset=utf-8');

12.       
13.     if (md5($_POST['verifyTest']) != Session::get('verify')){ 
14.       echo '验证码错误'; 
15.     } 
16.     else { 
17.       echo '验证码正确'; 
18.         } 
19.   } 
20.     
21.    //生成验证码 
22.    publicfunction verify() { 
23.           import("ORG.Util.Image"); 
24.           Image::buildImageVerify(); 
25.   } 
26.} 
27.?>

在对应的模板文件:Tpl\default\index目录下新建文件verifyTest.html,内容如下:
1.<scripttype='text/javascript'>
2.//重载验证码 
3.function freshVerify(){ 
4. document.getElementByIdx('verifyImg').src='__URL__/verify/'+Math.random(); 
5.} 
6.</script>
7.<form method='post'action='__URL__/verifyCheck'>
8.<input type='text'name='verifyTest'>
9.<img style='cursor:pointer' title='刷新验证码'src='__URL__/verify' id='verifyImg'onClick='freshVerify()'/>
10.<buttontype='submit'>确定</button>
11.</form>


联系我时,请说是在环球搜宝网看到的。
首页我的发布电话