本文实例讲述了thinkphp实现的验证码登录功能。分享给大家供大家参考,具体如下:
使用thinkphp自带的验证,实现登录页面的账号密码+验证码的验证
<?php
namespace admincontroller;
use thinkcontroller;
use thinkverify;
class logincontroller extends controller{
public function login(){
if($_post){
$obj = new verify();
if($obj->check(i('post.yanzhengma','','trim'))){
// 注释部分为另外一种从数据库中验证密码的方法
// $data['name'] = i('post.user_name');
// $data['psd'] = i('post.password');
// $row = m('user')->where($data)->find();
$name = i('post.user_name');
$psd = i('post.password');
$str = 'name ="'.$name. '" and tel = "'.$psd.'"';
var_dump($str);
$row = m('user')->where($str)->find();
if($row)
$this->redirect("index/index");
else
$this->redirect('login','',1,'用户名或密码错误');
}
else{
$this->redirect('login','',1,'验证码错误');
}
}
$this->display();
}
public function verifyimg(){
//设置验证码的宽高字体大小以及验证码的个数,设计其他的参照thinkverify里面的设置
$config=array(
'imagew' => 150,
'imageh' => 40,
'fontsize' => 20,
'length' => 4
);
$obj = new thinkverify($config);
$obj->entry();
}
}
表单部分
<form action="login" method="post">
<table valign="top" width="50%">
<tr><td colspan="2"><h4 style="letter-spacing:1px;font-size:16px;">rainman 网站管理后台</h4></td></tr>
<tr><td>管理员:</td><td><input type="text" name="user_name" value="" /></td></tr>
<tr><td>密 码:</td><td><input type="password" name="password" value="" /></td></tr>
<tr><td>验证码:</td>
<td><input type="text" name="yanzhengma" value="" style="width:80px;"/></td>
<td><img src="__url__/verifyimg" onclick="this.src='__url__/verifyimg/'+math.random()" alt=""/></td>
</tr>
<tr class="bt" align="center"><td> <input type="submit" value="登陆" /></td><td> <input type="reset" value="重填" /></td></tr>
</table>
</form>
更多关于thinkphp相关内容感兴趣的读者可查看本站专题:《thinkphp入门教程》、《thinkphp模板操作技巧总结》、《thinkphp常用方法总结》、《codeigniter入门教程》、《ci(codeigniter)框架进阶教程》、《zend framework框架入门教程》及《php模板技术总结》。
希望本文所述对大家基于thinkphp框架的php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!