`
crazymud
  • 浏览: 57417 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

thinkphp验证码的使用

阅读更多

      在thinkphp中使用验证码很容易,只要调用thinkphp现有的方法就可以。当然,php的GD库肯定是要开的。

首先,在写Action文件,如:IndexAction.class.php.

<?php
class IndexAction extends Action{
	//显示验证码
	public function verifyTest() {
	  $this->display();
	}
	
	//检验验证码是否正确
	public function verifyCheck() {
	  //防止页面乱码
          header('Content-type:text/html;charset=utf-8');
	  
	  if (md5($_POST['verifyTest']) != Session::get('verify')) {
	    echo '验证码错误';
	  }
	  else {
	    echo '验证码正确';
          }
	}
	
	// 生成验证码
	public function verify() {
            import("ORG.Util.Image");
            Image::buildImageVerify();
	}
}
?>

 在对应的模板文件:Tpl\default\index目录下新建文件verifyTest.html,内容如下:

<script type='text/javascript'>
//重载验证码
function freshVerify() {
  document.getElementById('verifyImg').src='__URL__/verify/'+Math.random();
}
</script>
<form method='post' action='__URL__/verifyCheck'>
<input type='text' name='verifyTest'>
<img style='cursor:pointer' title='刷新验证码' src='__URL__/verify' id='verifyImg' onClick='freshVerify()'/>
<button type='submit'>确定</button>
</form>

 view如下:

0
0
分享到:
评论
2 楼 haitaoyj0606 2010-10-22  
验证码库官网上下载嘛,
1 楼 龙翔天下 2010-07-01  
如果能留下验证码库就好了。

相关推荐

Global site tag (gtag.js) - Google Analytics