且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

codeigniter简单登录验证不工作

更新时间:2023-12-03 18:11:34

如果您需要更多的帮助,

***在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 ?>