使用codeigniter工作并在显示错误消息的登录页面中进行表单验证

时间:2015-11-21 13:27:44

标签: php codeigniter validation

我目前正在使用Codeigniter开展项目。作为其功能的一部分,我需要验证每当我有一个add函数等等。在实现这个时,我成功地做到了,但是当我在登录时尝试它时,它无法正常工作。我想要的是,每当验证失败时,登录页面内都会显示一条显示消息,因此用户将意识到,为什么它不会重定向到他或她想要的页面。这是我的代码:

控制器:

    public function index()
        {

                if($this->session->userdata('users_account_id') && $this->session->userdata('user_type')){
                    $user_type =  $this->session->userdata('user_type');
                    redirect('admin/master/'.$user_type.'');
                }
            $this->load->view('app/login');
        }
public function login ()
{
    $this->load->library('form_validation');

    $this->form_validation->set_rules('username', 'Username', 'trim|required');
    $this->form_validation->set_rules('password', 'Password', 'trim|required');

    if ($this->form_validation->run() == FALSE ) {
        $this->load->view('app/login');
    } else {
        session_start();
        if ($_POST) {
            $username = $_POST['username'];
            $password = $_POST['password'];

            $user = $this->account->login($username, $password);

            $this->session->set_userdata('username',$user['username']);             
            $this->session->set_userdata('users_account_id',$user['users_account_id']);             
            $this->session->set_userdata('user_type',$user['user_type']);
            $user_type = $user['user_type'];

            if ($user_type == "") {

                redirect('admin/index');
            }
            redirect('admin/master/'.$user_type.'');

        }
    }
}

查看(登录页面)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <!-- The above 3 meta tags *must* come first in the head; any other head content must come *after* these tags -->
    <meta name="description" content="">
    <meta name="author" content="">
    <link rel="shortcut icon" href="<?php echo base_url('image/logo.jpg')?>" />
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <title>Login Page</title>

    <!-- Bootstrap core CSS -->
    <?php echo link_tag('dist/css/bootstrap.min.css');?>
    <?php echo link_tag('css/style.css');?>
    <!-- Custom styles for this template -->
    <?php echo link_tag('dist/css/dashboard.css');?>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/font-awesome/4.4.0/css/font-awesome.min.css">
    <script src=<?php echo base_url('assets/js/ie-emulation-modes-warning.js')?>></script>
    <script src=<?php echo base_url('dist/js/drag.js')?>></script>
  </head>

  <body ng-app="GetDataModule">
    <div class="container-fluid">
        <?php echo validation_errors();?>
        <div class="row">

            <div class="col-md-12">
                <div class="row">
                    <div class="col-md-8">
                    </div>
                    <div class="col-md-3" id="form_BG">

                        <form role="form" action="<?php echo site_url('admin/login');?>" method="post">
                            <div class="form-group">

                                <label for="username">
                                    Username
                                </label>
                                <input type="text" name="username" class="form-control" id="username" placeholder="Username" ng-model="username" />
                            </div>
                            <div class="form-group">

                                <label for="password">
                                    Password
                                </label>
                                <input type="password" name="password" class="form-control" id="password" placeholder="Password" ng-model="password" />
                            </div>
                            <button type="submit" class="btn btn-lg btn-primary btn-block" ng-click="validate()">Submit</button>
                        </form>
                            <!-- <br /> -->
                            <!-- <a class="btn btn-sm btn-info" href="<?php echo base_url('index.php/admin/signup')?>">Sign Up</a> -->
                    </div>
                    <div class="col-md-2">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <!--End Of New Div Template -->


    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
    <script src=<?php echo base_url('dist/js/bootstrap.min.js')?>></script>
    <script src=<?php echo base_url('assets/js/vendor/holder.min.js')?>></script>
    <script src=<?php echo base_url('assets/js/ie10-viewport-bug-workaround.js')?>></script>
  </body>
</html>

模型

public function login ($username, $password) {
        $condition = array (
            'username' => $username,
            'password' => sha1($password)
        );

        $this->db->select();
        $this->db->from('users_account');
        $this->db->where($condition);

        $query = $this->db->get();

        return $query->first_row('array');
    }

如何解决这个问题,每当我登录失败时都会出现验证错误?我上面的代码没有给出任何错误,但是当记录失败时,它仍将保留在同一页面中,并且在登录成功之前不会重定向。任何帮助表示赞赏。非常感谢。

0 个答案:

没有答案