Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/231.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在登录页面中使用codeigniter进行表单验证不工作并显示错误消息_Php_Codeigniter_Validation - Fatal编程技术网

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

Php 在登录页面中使用codeigniter进行表单验证不工作并显示错误消息,php,codeigniter,validation,Php,Codeigniter,Validation,我目前正在使用Codeigniter进行一个项目。作为其功能的一部分,每当我有一个add函数时,我都需要进行验证,等等。在实现这一点时,我成功地做到了这一点,但当我在登录中尝试它时,它不起作用。我想要的是,每当验证失败时,登录页面中也会出现一条显示消息,因此用户会知道,为什么不重定向到他或她想要的页面。这是我的密码: 控制器: public function index() { if($this->session->userd

我目前正在使用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.'');

        }
    }
}
查看(登录页面)


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

发布您的型号代码<代码>功能登录名!!在
登录函数中添加验证,而不是在
索引中添加验证。
@Saty:已使用模型中的函数进行更新在登录函数中写入验证代码控制器中的哪一个?
<!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');
    }