能否請你幫我明白爲什麼我的代碼將不會顯示setmessage:笨:不顯示登錄錯誤消息
$this->form_validation->set_message('Sorry %s is not correct.');
的驗證高興地表明它們是必需的:
home.php - >控制器
<?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
登錄_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.');
- 我不確定什麼第二對必須是
謝謝 - 該代碼是從一個教程 – 2011-03-25 17:51:19
不要相信互聯網上的一切:P – 2011-03-28 04:08:10