Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/296.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进行登录表单验证。代码以前工作正常,但当我做了一些修改后,它现在就不工作了 当我尝试键入错误的密码或用户名时,错误消息显示为正常,但当我键入正确的用户名和密码时,$this->form\u validation->run()只需给我一个空的validation\u errors()字符串 我试图恢复我的旧工作控制器php,但这个错误阻止了 有关详细代码,请访问 用户控制器 登录视图 用户模型 以下仅粘贴相关代码: 控制器: <?php class Use

我正在使用codeigniter进行登录表单验证。代码以前工作正常,但当我做了一些修改后,它现在就不工作了

当我尝试键入错误的密码或用户名时,错误消息显示为正常,但当我键入正确的用户名和密码时,
$this->form\u validation->run()
只需给我一个空的
validation\u errors()
字符串

我试图恢复我的旧工作控制器php,但这个错误阻止了

有关详细代码,请访问

  • 用户控制器
  • 登录视图
  • 用户模型
以下仅粘贴相关代码:

控制器:

<?php
class User extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('user_model');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('cookie');
    }
    function login(){

        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|md5|callback_password_check');
        $this->_username = $this->input->post('username');                //用户名


        $remember_me = $this->input->post('remember_me');
        $json_string = $this->input->cookie('userinfo');
        $userinfo_json = json_decode($json_string);

        if(isset($userinfo_json->username)){
            if ($this->username_check($userinfo_json->username)){
                $this->user_model->login($userinfo);
                redirect('admin/dashboard');
            }
        }


        if ($this->form_validation->run() == FALSE){
            $this->load->view('account/login');
        } else {
            $userinfo=$this->user_model->get_by_username($this->_username);
            $this->user_model->login($userinfo);
            if($remember_me=="on"){$this->user_model->write_session($userinfo);}
            redirect('admin/dashboard');
        }


    }
    function username_check($username){
        if ($this->user_model->get_by_username($username)){
            return TRUE;
        }else{
            $this->form_validation->set_message('username_check', 'User name not exist.');
            return FALSE;
        }
    }
    function password_check($password) {
        $password = md5($password);
        if ($this->user_model->password_check($this->_username, $password)){
            return TRUE;
        }else{
            $this->form_validation->set_message('password_check', 'Incorrect username or paswsword.');
            return FALSE;
        }
    }
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login - <?=$this->admin_model->get_title();?></title>
    <?php $this->load->view('gy/head');?>
</head>
<body>
    <?php $this->load->view('gy/header');?>
    <div class="hero-unit header">
        <div class="container">
            <div style="text-align:center;">
                <h1>Sign in</h1>    
                <p class="lead">Log into <?=$this->admin_model->get_title();?>.</p>
            </div>
        </div>
    </div>
    <div class="container">
        <div class="span5 offset3">
        <?php if(validation_errors() !== '' || @(!$err_message == '')){ ?>
        <div class="alert alert-error fade in">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Error!</strong> <?=validation_errors('<span>','</span>');?> <?=@$err_message?>
        </div>
        <?php } ?>
        <?php if(@$message!=''){ ?>
        <div class="alert fade in alert-success">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Success!</strong> <?=$message?>
        </div>
        <?php } ?>
            <?php echo form_open('login',array('class'=>'form-horizontal')); ?>
                <div class="control-group">
                    <label class="control-label" for="username">User name</label>
                    <div class="controls">
                        <input type="text" id="username" name="username" placeholder="User name">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="password">Password</label>
                    <div class="controls">
                        <input type="password" id="password" name="password" placeholder="Password">
                    </div>
                </div>
                <label for="remember_me" class="checkbox">
                    <input type="checkbox" id="remember_me" name="remember_me"> Remember me (30 days)
                </label>
                <div style="text-align:center;">
                    <input type="submit" name="submit" value="Log in" class="btn btn-primary">
                </div>
        </Form>
    </div>
    </div>
    <?php $this->load->view('gy/footer');?>
</body>
</html>
<?php 
class user_model extends CI_Model {
    public function __construct()
  {
    parent::__construct();
    $this->load->database();
    $this->load->library('session');

  }
     function login($userinfo)
     {
         $data = array('username'=>$userinfo->username,
                       'user_id'=>$userinfo->id, 
                       'role'=>$userinfo->role,
                       'logged_in'=>TRUE);
         $this->session->set_userdata($data);
     }
     function write_session($userinfo)
     {
        $user_json = json_encode($userinfo);
        $cookie = array(
            'name'   => 'userinfo',
            'value'  => $user_json,
            'expire' => '2592000',
            'secure' => TRUE
        );
        $this->input->set_cookie($cookie);
     }
     function get_by_username($username)
     {
         $this->db->where('username', $username);
         $query = $this->db->get('users');
         if ($query->num_rows() == 1)
         {
             return $query->row();
         }
         else
         {
             return FALSE;
         }
     }
     function password_check($username, $password)
     {                
         if($user = $this->get_by_username($username))
         {
             return $user->password == $password ? TRUE : FALSE;
         }
         return FALSE;
     }
}
$this->form_validation->set_rules('username','Username','callback_space_check|required|xss_clean|callback_username_check');

function space_check($input){
   if(strpos(' ',$input)===false){
       return TRUE;
    }else{
            $this->form_validation->set_message('space_check', '%s contains space.');
            return FALSE;
        }
}

登录

登录

错误 成功 用户名 密码 记住我(30天)
型号:

<?php
class User extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('user_model');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('cookie');
    }
    function login(){

        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|md5|callback_password_check');
        $this->_username = $this->input->post('username');                //用户名


        $remember_me = $this->input->post('remember_me');
        $json_string = $this->input->cookie('userinfo');
        $userinfo_json = json_decode($json_string);

        if(isset($userinfo_json->username)){
            if ($this->username_check($userinfo_json->username)){
                $this->user_model->login($userinfo);
                redirect('admin/dashboard');
            }
        }


        if ($this->form_validation->run() == FALSE){
            $this->load->view('account/login');
        } else {
            $userinfo=$this->user_model->get_by_username($this->_username);
            $this->user_model->login($userinfo);
            if($remember_me=="on"){$this->user_model->write_session($userinfo);}
            redirect('admin/dashboard');
        }


    }
    function username_check($username){
        if ($this->user_model->get_by_username($username)){
            return TRUE;
        }else{
            $this->form_validation->set_message('username_check', 'User name not exist.');
            return FALSE;
        }
    }
    function password_check($password) {
        $password = md5($password);
        if ($this->user_model->password_check($this->_username, $password)){
            return TRUE;
        }else{
            $this->form_validation->set_message('password_check', 'Incorrect username or paswsword.');
            return FALSE;
        }
    }
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login - <?=$this->admin_model->get_title();?></title>
    <?php $this->load->view('gy/head');?>
</head>
<body>
    <?php $this->load->view('gy/header');?>
    <div class="hero-unit header">
        <div class="container">
            <div style="text-align:center;">
                <h1>Sign in</h1>    
                <p class="lead">Log into <?=$this->admin_model->get_title();?>.</p>
            </div>
        </div>
    </div>
    <div class="container">
        <div class="span5 offset3">
        <?php if(validation_errors() !== '' || @(!$err_message == '')){ ?>
        <div class="alert alert-error fade in">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Error!</strong> <?=validation_errors('<span>','</span>');?> <?=@$err_message?>
        </div>
        <?php } ?>
        <?php if(@$message!=''){ ?>
        <div class="alert fade in alert-success">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Success!</strong> <?=$message?>
        </div>
        <?php } ?>
            <?php echo form_open('login',array('class'=>'form-horizontal')); ?>
                <div class="control-group">
                    <label class="control-label" for="username">User name</label>
                    <div class="controls">
                        <input type="text" id="username" name="username" placeholder="User name">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="password">Password</label>
                    <div class="controls">
                        <input type="password" id="password" name="password" placeholder="Password">
                    </div>
                </div>
                <label for="remember_me" class="checkbox">
                    <input type="checkbox" id="remember_me" name="remember_me"> Remember me (30 days)
                </label>
                <div style="text-align:center;">
                    <input type="submit" name="submit" value="Log in" class="btn btn-primary">
                </div>
        </Form>
    </div>
    </div>
    <?php $this->load->view('gy/footer');?>
</body>
</html>
<?php 
class user_model extends CI_Model {
    public function __construct()
  {
    parent::__construct();
    $this->load->database();
    $this->load->library('session');

  }
     function login($userinfo)
     {
         $data = array('username'=>$userinfo->username,
                       'user_id'=>$userinfo->id, 
                       'role'=>$userinfo->role,
                       'logged_in'=>TRUE);
         $this->session->set_userdata($data);
     }
     function write_session($userinfo)
     {
        $user_json = json_encode($userinfo);
        $cookie = array(
            'name'   => 'userinfo',
            'value'  => $user_json,
            'expire' => '2592000',
            'secure' => TRUE
        );
        $this->input->set_cookie($cookie);
     }
     function get_by_username($username)
     {
         $this->db->where('username', $username);
         $query = $this->db->get('users');
         if ($query->num_rows() == 1)
         {
             return $query->row();
         }
         else
         {
             return FALSE;
         }
     }
     function password_check($username, $password)
     {                
         if($user = $this->get_by_username($username))
         {
             return $user->password == $password ? TRUE : FALSE;
         }
         return FALSE;
     }
}
$this->form_validation->set_rules('username','Username','callback_space_check|required|xss_clean|callback_username_check');

function space_check($input){
   if(strpos(' ',$input)===false){
       return TRUE;
    }else{
            $this->form_validation->set_message('space_check', '%s contains space.');
            return FALSE;
        }
}

我怀疑修剪规则。它不返回布尔值,我想您还没有为该规则设置任何错误消息。验证成功后,应在用户名和密码上运行trim()。如果您只想检查输入是否有空格,可以添加一个规则来使用strpos()进行检查,但也可以在自定义回调中进行检查。只需记住使用相同的运算器'==',而不是相等的'='

控制器:

<?php
class User extends CI_Controller {
    public function __construct()
    {
        parent::__construct();
        $this->load->library('form_validation');
        $this->load->model('user_model');
        $this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('cookie');
    }
    function login(){

        $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean|callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'trim|required|md5|callback_password_check');
        $this->_username = $this->input->post('username');                //用户名


        $remember_me = $this->input->post('remember_me');
        $json_string = $this->input->cookie('userinfo');
        $userinfo_json = json_decode($json_string);

        if(isset($userinfo_json->username)){
            if ($this->username_check($userinfo_json->username)){
                $this->user_model->login($userinfo);
                redirect('admin/dashboard');
            }
        }


        if ($this->form_validation->run() == FALSE){
            $this->load->view('account/login');
        } else {
            $userinfo=$this->user_model->get_by_username($this->_username);
            $this->user_model->login($userinfo);
            if($remember_me=="on"){$this->user_model->write_session($userinfo);}
            redirect('admin/dashboard');
        }


    }
    function username_check($username){
        if ($this->user_model->get_by_username($username)){
            return TRUE;
        }else{
            $this->form_validation->set_message('username_check', 'User name not exist.');
            return FALSE;
        }
    }
    function password_check($password) {
        $password = md5($password);
        if ($this->user_model->password_check($this->_username, $password)){
            return TRUE;
        }else{
            $this->form_validation->set_message('password_check', 'Incorrect username or paswsword.');
            return FALSE;
        }
    }
}
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Login - <?=$this->admin_model->get_title();?></title>
    <?php $this->load->view('gy/head');?>
</head>
<body>
    <?php $this->load->view('gy/header');?>
    <div class="hero-unit header">
        <div class="container">
            <div style="text-align:center;">
                <h1>Sign in</h1>    
                <p class="lead">Log into <?=$this->admin_model->get_title();?>.</p>
            </div>
        </div>
    </div>
    <div class="container">
        <div class="span5 offset3">
        <?php if(validation_errors() !== '' || @(!$err_message == '')){ ?>
        <div class="alert alert-error fade in">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Error!</strong> <?=validation_errors('<span>','</span>');?> <?=@$err_message?>
        </div>
        <?php } ?>
        <?php if(@$message!=''){ ?>
        <div class="alert fade in alert-success">
            <a href="#" class="close" data-dismiss="alert">&times;</a>
            <strong>Success!</strong> <?=$message?>
        </div>
        <?php } ?>
            <?php echo form_open('login',array('class'=>'form-horizontal')); ?>
                <div class="control-group">
                    <label class="control-label" for="username">User name</label>
                    <div class="controls">
                        <input type="text" id="username" name="username" placeholder="User name">
                    </div>
                </div>
                <div class="control-group">
                    <label class="control-label" for="password">Password</label>
                    <div class="controls">
                        <input type="password" id="password" name="password" placeholder="Password">
                    </div>
                </div>
                <label for="remember_me" class="checkbox">
                    <input type="checkbox" id="remember_me" name="remember_me"> Remember me (30 days)
                </label>
                <div style="text-align:center;">
                    <input type="submit" name="submit" value="Log in" class="btn btn-primary">
                </div>
        </Form>
    </div>
    </div>
    <?php $this->load->view('gy/footer');?>
</body>
</html>
<?php 
class user_model extends CI_Model {
    public function __construct()
  {
    parent::__construct();
    $this->load->database();
    $this->load->library('session');

  }
     function login($userinfo)
     {
         $data = array('username'=>$userinfo->username,
                       'user_id'=>$userinfo->id, 
                       'role'=>$userinfo->role,
                       'logged_in'=>TRUE);
         $this->session->set_userdata($data);
     }
     function write_session($userinfo)
     {
        $user_json = json_encode($userinfo);
        $cookie = array(
            'name'   => 'userinfo',
            'value'  => $user_json,
            'expire' => '2592000',
            'secure' => TRUE
        );
        $this->input->set_cookie($cookie);
     }
     function get_by_username($username)
     {
         $this->db->where('username', $username);
         $query = $this->db->get('users');
         if ($query->num_rows() == 1)
         {
             return $query->row();
         }
         else
         {
             return FALSE;
         }
     }
     function password_check($username, $password)
     {                
         if($user = $this->get_by_username($username))
         {
             return $user->password == $password ? TRUE : FALSE;
         }
         return FALSE;
     }
}
$this->form_validation->set_rules('username','Username','callback_space_check|required|xss_clean|callback_username_check');

function space_check($input){
   if(strpos(' ',$input)===false){
       return TRUE;
    }else{
            $this->form_validation->set_message('space_check', '%s contains space.');
            return FALSE;
        }
}

任何接受一个参数的本机PHP函数都可以作为规则使用,如htmlspecialchars、trim、MD5等@fabiano Araujototal不好意思,我会回到我的洞穴,冬眠到下一季。抱歉,还是不明白,哪里出错了?我现在也有同样的问题。