php - CodeIgniter:不显示登录错误消息

标签 php codeigniter

您能帮我解决为什么我的代码不会显示 setmessage:

$this->form_validation->set_message('抱歉 %s 不正确。');

验证很高兴地表明它们是必需的:

home.php -> Controller

<?php

ob_start();

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Home extends CI_Controller {

function __construct(){
    parent::__construct();
}

function index(){
    $this->login();
}

public function login()
{
    //Loads The Page Template
    $this->template->set('title','Admin Login');

    //Validation Check

    $this->form_validation->set_rules('username','Username','required|trim|max_length[50]|xss_clean');
    $this->form_validation->set_rules('password','Password','required|trim|max_length[200]|xss_clean|callback_checkUsernamePassword');

    if($this->form_validation->run() == FALSE){

        $this->template->load('template','admin/admin_login');  

    }else{
        extract($_POST); // Gets data from form and creates vars
        $user_id = $this->login_model->check_login($username,$password);

        if(! $user_id || $password){ // != If username or password are not correct
            $this->session->set_flashdata('login_error',TRUE); //does not add the non valid login to the session
            $this->form_validation->set_message('Sorry %s is not correct.');
            redirect('admin');

        }else{
            $this->session->set_userdata('logged_in',TRUE);
            $this->session->set_userdata('user_id',$user_id);
            redirect('admin/dashboard');
        }
    }

}

function logout(){
    $this->session->unset_userdata('logged_in');
    echo 'You have now been logged out';
    redirect('admin');
}


}

//End of file home.php 
//Location: ./application/controllers/admin/home.php 

login_model.php -> 模型

<?php

 class Login_model extends CI_Model
   {

function __construct()
{
parent::__construct();
}

function Login_model(){
    parent::Model();
}

function check_login($username,$password){
    $MD5_password = md5($password); // Tells the db that the password is a MD5 #

    $query_str ="SELECT id FROM users WHERE email = ? and password = ?"; // Tells the db that this is a query

    $result = $this->db->query($query_str, array($username, $MD5_password)); // Result

    //If it is all correct

    if($result->num_rows() == 1){

        return $result->row(0)->id;

    }else{
        return false;
    }
}
 }
?>

我尝试过以下方法:

$lang['error'] = "Sorry your %s is incorrect."; 

- This is set in the lang file

$this->form_validation->set_message('error','Sorry %s is not correct.'); 
  • 我不确定第二段是什么

最佳答案

您真的真的应该阅读用户指南。你的逻辑不正确。例如,您没有使用回调。这就是为什么您的错误消息不显示的原因。我写了一些评论供您阅读。

public function login()
{
    $this->template->set('title','Admin Login');

    $this->form_validation->set_rules('username','Username', 'required|trim|max_length[50]|xss_clean');
    // You aren't using the callback here.
    $this->form_validation->set_rules('password','Password', 'required|trim|max_length[200]|xss_clean|callback_checkUsernamePassword');

    if($this->form_validation->run() == FALSE){

        $this->template->load('template','admin/admin_login');  

    }else{
        // You shouldn't be adding messages when the validation has already passed. The setting should be when the validation is false.
        extract($_POST);
        $user_id = $this->login_model->check_login($username,$password);

        if(! $user_id || $password){
            $this->session->set_flashdata('login_error',TRUE); //does not add the non valid login to the session
            $this->form_validation->set_message('Sorry %s is not correct.');
            redirect('admin');

        }else{
            $this->session->set_userdata('logged_in',TRUE);
            $this->session->set_userdata('user_id',$user_id);
            redirect('admin/dashboard');
        }
    }
}

这是你应该做的。我不会编写所有代码,但会给您一个想法。

public function login()
{
    $this->template->set('title','Admin Login');

    $this->form_validation->set_rules('username','Username', 'required|trim|max_length[50]|xss_clean');
    $this->form_validation->set_rules('password','Password', 'required|trim|max_length[200]|xss_clean|callback_checkUsernamePassword');

    if($this->form_validation->run() == TRUE){
        $this->session->set_userdata('logged_in',TRUE);
        $this->session->set_userdata('user_id',$user_id);
        redirect('admin/dashboard');
    }

    $this->template->load('template','admin/admin_login'); 
}

public function checkUsernamePassword() {
        extract($_POST); // Gets data from form and creates vars
        $user_id = $this->login_model->check_login($username,$password);

        if(! $user_id || $password){ // != If username or password are not correct
            $this->session->set_flashdata('login_error',TRUE); //does not add the non valid login to the session
            $this->form_validation->set_message('checkUsernamePassword', 'Sorry %s is not correct.');
            return FALSE

        }else{
            $this->session->set_userdata('logged_in',TRUE);
            $this->session->set_userdata('user_id',$user_id);
            redirect('admin/dashboard');
        }

}

关于php - CodeIgniter:不显示登录错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5427697/

相关文章:

php - 如何调试 exec() 问题?

codeigniter - 如何在 twitter 上为 CodeIgniter 预先输入额外的参数

CodeIgniter框架: 500 Internal Server Error

javascript - 使用 AJAX/jQuery 异步下载文件

php - 使用视频ID从PHP API获得YouTube视频质量

php - PHP : {'error' : "Missing ' inputs' or 'instances' key"} 中的休息 API

php - Codeigniter不显示文件上传错误

MySQL 事件记录查询忽略大小写

php - 需要更新 codeigniter 上的用户表密码字段

php - 在 Wordpress 中,如何根据用户所在的页面更改链接的目标?