codeigniter简单登录验证不工作 [英] codeigniter simple login validation not working

查看:102
本文介绍了codeigniter简单登录验证不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是网路程式设计的初学者,请随身携带。



我尝试建立一个可以进行验证的登入系统:


  1. 电子邮件是必填项,且必须有效

  2. 必须输入密码

  3. 如果登录失败,我将为用户创建一个跨度

我可以完成第一&第二个很容易使用codeigniter的表单验证。但是,第三个不是那么容易,因为我需要从数据库检查它。



所以我试图创建一个这样的代码:

  function login(){
$ data ['validation'] = TRUE;

$ this-> form_validation-> set_rules('txt_email','Email','required | valid_email');
$ this-> form_validation-> set_rules('txt_password','Password','required');

if($ this-> form_validation-> run()!== FALSE)
{
$ log_in = $ this-> umat_m-> log_in $ this-> input-> post('txt_email'),$ this-> input-> post('txt_password'))
if($ log_in!== FALSE)
{
$ data ['validation'] = TRUE;
$ _SESSION ['username'] = $ this-> input-> post('txt_email');
redirect('backend / umat / index');
}

else
$ data ['validation'] = FALSE;
}

$ this-> load-> view('backend / login_v',$ data);
}

HTML:

 <?php echo form_submit('btn_submit','Log In','class =btn btn-primary'); ?> < br /> 
<?php
if($ validation === FALSE)
echo'< span class =label label-important> Wrong Username / Password< / span>
else
echo'< span class =label label-important><?php echo validation_errors(); ?>< / span>';
?>

我试图做的是如果用户已经填写电子邮件&密码字段,但值错误,那么我将回显第一个span。



但是,这个代码仍然不工作。当用户填写错误的值(但不为空)时,会回显错误的用户名/密码跨度,但是当文本字段为空时,代码指示符的表单验证不会显示任何消息(这意味着第二个回显将不会显示)。

解决方案

如果您需要更多的帮助,

最好在Controller中处理:



控制器

  function login()
{
//设置默认值
$ data ['error'] ='';

$ this-> form_validation-> set_rules('txt_email','Email','required | valid_email');
$ this-> form_validation-> set_rules('txt_password','Password','required');

if($ this-> form_validation-> run()!== FALSE)
{
$ log_in = $ this-> umat_m-> log_in
$ this-> input-> post('txt_email'),
$ this-> input-> post('txt_password')
);

if($ log_in!== FALSE)
{
$ _SESSION ['username'] = $ this-> input-> post('txt_email');
redirect('backend / umat / index');
} else {
//设置您的错误消息
$ data ['error'] ='错误的用户名/密码';
}
} else {
//设置验证错误
$ data ['error'] = validation_errors();
}

$ this-> load-> view('backend / login_v',$ data);
}

查看

 <?php if($ error):?> 
< span class =label label-important><?php echo $ error; ?>< / span>
<?php endif?>


Im a beginner in web programming, so please bear with me.

I tried to create a login system that will do those validation :

  1. The email is required and must be valid
  2. The password is required
  3. If the login failed, i will create a span for the user

I can accomplish the 1st & 2nd easily using codeigniter's form validation. However, the 3rd is not that easy because i need to check it first from the DB.

So i tried to create a code like this :

function login() {
        $data['validation'] = TRUE;

        $this->form_validation->set_rules('txt_email', 'Email', 'required|valid_email');
        $this->form_validation->set_rules('txt_password', 'Password', 'required');

        if($this->form_validation->run() !== FALSE)
        {
            $log_in = $this->umat_m->log_in($this->input->post('txt_email'), $this->input->post('txt_password'));
            if($log_in !== FALSE)
            {
                $data['validation'] = TRUE;
                $_SESSION['username'] = $this->input->post('txt_email');
                redirect('backend/umat/index');
            }

            else
                $data['validation'] = FALSE;
        }

        $this->load->view('backend/login_v', $data);
    }

And the HTML :

<?php echo form_submit('btn_submit', 'Log In', 'class = "btn btn-primary"'); ?> <br/>
                        <?php
                            if($validation === FALSE)
                                echo '<span class="label label-important">Wrong Username/Password</span>';
                            else
                                echo '<span class="label label-important"><?php echo validation_errors(); ?></span>';
                        ?>

What i tried to do is if the user already fill the Email & Password field but the value is wrong then i will echo the first span.

However, this code is still not working. When the user fill the wrong value (but not empty), the Wrong Username/Password span is echoed but when the textfield is empty, the codeigniter's form validation dont show any message (that means the 2nd echo is never showed).

Any help is appreciated, and please just ask me if you need something more.

解决方案

It is better to handle this in Controller:

Controller:

function login()
{
    // Set the default value
    $data['error'] = '';

    $this->form_validation->set_rules('txt_email', 'Email', 'required|valid_email');
    $this->form_validation->set_rules('txt_password', 'Password', 'required');

    if($this->form_validation->run() !== FALSE)
    {
        $log_in = $this->umat_m->log_in(
            $this->input->post('txt_email'),
            $this->input->post('txt_password')
        );

        if($log_in !== FALSE)
        {
            $_SESSION['username'] = $this->input->post('txt_email');
            redirect('backend/umat/index');
        } else {
            // Set your error message
            $data['error'] = 'Wrong Username/Password';
        }
    } else {
        // Set the validation errors
        $data['error'] = validation_errors();
    }

    $this->load->view('backend/login_v', $data);
}

View:

<?php if ($error): ?>
    <span class="label label-important"><?php echo $error; ?></span>
<?php endif ?>

这篇关于codeigniter简单登录验证不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆