尝试设置回叫功能,但由于某种原因它无法正常工作。不知道我在这里做错了什么。
在下面的控制器中查找registration()和_email_check($ str)函数。
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class Register extends CI_Controller {
public function __construct()
{
parent::__construct();
$this -> load -> model('user_model');
}
public function index()
{
if (($this -> session -> userdata('user_name') != "")) {
redirect('dashboard');
}
else
{
$data['title'] = 'Register';
$this -> load -> view('shared/header_view', $data);
$this -> load -> view("registration.php", $data);
$this -> load -> view('shared/footer_view', $data);
}
}
public function login()
{
$email = $this -> input -> post('email');
$password = $this -> input -> post('pass');
$result = $this -> user_model -> login($email, $password);
if ($result)
redirect('dashboard');
else
$this -> index();
}
public function registration()
{
$this -> load -> library('form_validation');
// field name, error message, validation rules
$this -> form_validation -> set_rules('user_name', 'User Name', 'trim|required|min_length[4]|xss_clean');
$this -> form_validation -> set_rules('email_address', 'Your Email', 'trim|required|valid_email', 'callback__email_check');
$this -> form_validation -> set_rules('password', 'Password', 'trim|required|min_length[4]|max_length[32]');
$this -> form_validation -> set_rules('con_password', 'Password Confirmation', 'trim|required|matches[password]');
if ($this -> form_validation -> run() == FALSE)
{
$this -> index();
}
else
{
$this -> user_model -> add_user();
$email = $this -> input -> post('email');
$password = $this -> input -> post('pass');
$result = $this -> user_model -> login($email, $password);
if ($result)
{
redirect('dashboard');
}
}
}
// Checks that Email Address is not in use
public function _email_check($str)
{
// return true if the address is indeed a new address
$this -> db -> where('email', $str);
$found = $this -> db -> get('user') -> num_results(); // this returns the number of rows having the same address.
if ($found!=0)
{
$this -> form_validation -> set_message('email_check', 'Email Address is already in use');
return false; // more than 0 rows found. the callback fails.
}
else
{
return true; // 0 rows found. callback is ok.
}
}
public function logout()
{
$newdata = array('user_id' => '', 'user_name' => '', 'user_email' => '', 'logged_in' => FALSE, );
$this -> session -> unset_userdata($newdata);
$this -> session -> sess_destroy();
redirect(base_url());
}
}
如果需要,这是MODEL:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
class User_model extends CI_Model {
public function __construct() {
parent::__construct();
}
function login($email, $password)
{
$this -> db -> where("email", $email);
$this -> db -> where("password", $password);
$query = $this -> db -> get("user");
if ($query -> num_rows() > 0)
{
foreach ($query->result() as $rows)
{
//add all data to session
$newdata = array(
'user_id' => $rows -> id,
'user_name' => $rows -> username,
'user_email' => $rows -> email,
'logged_in' => TRUE,
);
}
$this -> session -> set_userdata($newdata);
return true;
}
return false;
}
public function add_user()
{
$data = array(
'username' => $this -> input -> post('user_name'),
'email' => $this -> input -> post('email_address'),
'password' => $this -> input -> post('password'),
);
$this -> db -> insert('user', $data);
}
}
答案 0 :(得分:3)
呃......刚刚告诉你要排除故障,问题是你正在调用回调错误。
$this -> form_validation -> set_rules('email_address', 'Your Email', 'trim|required|valid_email|callback__email_check')