登陆表单密码验证不能,求解决~
控制器部分:function index(){
$this->load->helper(array('form', 'url'));
$this->load->library('form_validation');
$this->form_validation->set_rules('username', '用户名', 'trim|required|max_length|xss_clean|callback_username_check');
$this->form_validation->set_rules('password', '密码', 'trim|required|xss_clean|md5|callback_username_check');
if ($this->form_validation->run() == TRUE){
redirect('http://localhost/Doc/index.php/Operation'); //表单验证后跳转
}else{
$this->load->view('loginform_view');
}
}
function username_check($username,$password){
$this->load->database();
$query = $this->db->query("SELECT * FROM admin WHERE name = '$username' AND pw = '$password'");
if( $query->num_rows()==1 ){
return TRUE;
}else{
$this->form_validation->set_message('username_check','%s 错误.');
return FALSE;
}
视图部分:
<div id='login_form'>
<form action="http://localhost/Doc/index.php/login" method="post" accept-charset="utf-8">
<h5>Userame</h5>
<input type="text" name="username" size="20" value="<?php echo set_value('username'); ?>"/>
<?php echo form_error('username');?>
<h5>Password</h5>
<input type="password" name="password" size="20" />
<?php echo form_error('password');?>
<input type="submit" value="Submit" />
</form>
</div>
问题出在楼主输入了正确的密码以后也依然显示错误,无法正确跳转。
打印set_rules部分的MD5 值 和 数据库里的MD5值是相同的,但是就是没法过。。
啥原因呢?求解答
本帖最后由 huboo82 于 2011-12-14 16:21 编辑
你的callback的方法能打印一下用户名和密码两个参数都有的吗? 还有
'%s 错误.'
应该这样用
sprintf('%s 错误.', '(任意提示内容)')
'(任意提示内容)'会替换掉里面的%s。 用这段代码可以显示$username 和$password 两个值
function username_check($username,$password){
$this->form_validation->set_message('username_check',"$username"."$password");
return FALSE;
}
能够在form_error返回的信息里显示出在表单输入的username和经过md5转换的password值 完全不明白原因啊~ 很郁闷 好吧~ 我错了。。。
貌似回调函数不能这样用 我是新手,我来说说我的看法,,你的index()里没有包函你下面的username_check ()方法呀?怎么通过数据库验证。 lcb21 发表于 2011-12-15 10:21 static/image/common/back.gif
我是新手,我来说说我的看法,,你的index()里没有包函你下面的username_check ()方法呀?怎么通过数据库 ...
必需要包含么? 手册上的例子里也没包含啊? 同一控制器中的函数不能相互调用么?
实际的问题是回调函数只能接收一个参数,并且是一次表单验证规则调用了一次,所以就算能接收两个参数,也不是同时的,没法验证账户密码组合。最后改成直接用input->post()来验证组合就OK了。
页:
[1]