实现 Codeigniter 登录的最佳方式
best way to implement Codeigniter login
这是我的登录控制器索引函数:
public function index() {
$data['title'] = 'Login';
$this->load->view('login_form', $data);
}
那么这是我的 validate_credentials 函数:
public function validate_credentials() {
$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->index();
} else {
$this->load->model('user_model');
if ($this->user_model->checkUserAccess($this->input->post('username'))) { // checks if user has access to the site
if ($this->ldapAuth()) { // checks if successful authentication with LDAP server
$email = $this->user_model->getEmail($this->input->post('username'));
$gpcid = $this->user_model->getUserGPC($this->input->post('username'));
$this->user_model->updateTimestamp('users', 'DateLastLogin', 'UserID', $this->input->post('username'));
$data = array (
'username' => $this->input->post('username'),
'gpcid' => $gpcid,
'email' => $email,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('profile');
} else { // unsuccessful login
$error = "Username and/or Password is incorrect";
$data['title'] = 'Login';
$data['message'] = $error;
$this->load->view('login_form', $data);
}
} else {
$error = "You do not have access to the site";
$data['title'] = 'Login';
$data['message'] = $error;
$this->load->view('login_form', $data);
}
}
}
有没有办法只调用索引函数并传递错误消息而不是多次加载视图?
$data['title'] = 'Login';
$data['message'] = $error;
$this->load->view('login_form', $data);
您必须将它重定向到索引函数,而不是加载视图文件。并使用 Session flash data 来实现这一点。
喜欢,
控制器
$this->session->set_flashdata( 'error', 'Username and/or Password is incorrect' );
redirect('index');
要在 login_form.php 查看文件上打印消息,
查看 (login_form.php)
<?php if($this->session->flashdata('error')){
echo $this->session->flashdata('message');
}?>
你的控制器
public function validate_credentials() {
$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->index();
} else {
$this->load->model('user_model');
if ($this->user_model->checkUserAccess($this->input->post('username'))) { // checks if user has access to the site
if ($this->ldapAuth()) { // checks if successful authentication with LDAP server
$email = $this->user_model->getEmail($this->input->post('username'));
$gpcid = $this->user_model->getUserGPC($this->input->post('username'));
$this->user_model->updateTimestamp('users', 'DateLastLogin', 'UserID', $this->input->post('username'));
$data = array (
'username' => $this->input->post('username'),
'gpcid' => $gpcid,
'email' => $email,
'is_logged_in' => true
);
$this->session->set_flashdata($data);
redirect('profile');
} else {
$userdata = array (
'error' =>'Username and/or Password is incorrect',
);
$this->session->set_flashdata($userdata);
redirect('redirect url here'); // here is your url for index
}
} else {
$userdata = array (
'error' =>'You do not have access to the site',
);
$this->session->set_flashdata($userdata);
redirect('redirect url here'); // here is your url for index
}
}
}
控制器
public function loginaction() {
$this->load->library('session');
$a = $this->input->post('email');
$b = trim($this->input->post('password'));
$b1 = md5($b);
$c = 1;
$data = $this->userdata->userlogin($a, $b1, $c);
if($data) {
echo true;
foreach ($data as $login) {
$uid = $this->session->set_userdata('logId', $login->usr_id);
}
} else {
echo "Invalid username or password..!!!!";
$a = $this->input->post('email');
}
}
这是我的登录控制器索引函数:
public function index() {
$data['title'] = 'Login';
$this->load->view('login_form', $data);
}
那么这是我的 validate_credentials 函数:
public function validate_credentials() {
$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->index();
} else {
$this->load->model('user_model');
if ($this->user_model->checkUserAccess($this->input->post('username'))) { // checks if user has access to the site
if ($this->ldapAuth()) { // checks if successful authentication with LDAP server
$email = $this->user_model->getEmail($this->input->post('username'));
$gpcid = $this->user_model->getUserGPC($this->input->post('username'));
$this->user_model->updateTimestamp('users', 'DateLastLogin', 'UserID', $this->input->post('username'));
$data = array (
'username' => $this->input->post('username'),
'gpcid' => $gpcid,
'email' => $email,
'is_logged_in' => true
);
$this->session->set_userdata($data);
redirect('profile');
} else { // unsuccessful login
$error = "Username and/or Password is incorrect";
$data['title'] = 'Login';
$data['message'] = $error;
$this->load->view('login_form', $data);
}
} else {
$error = "You do not have access to the site";
$data['title'] = 'Login';
$data['message'] = $error;
$this->load->view('login_form', $data);
}
}
}
有没有办法只调用索引函数并传递错误消息而不是多次加载视图?
$data['title'] = 'Login';
$data['message'] = $error;
$this->load->view('login_form', $data);
您必须将它重定向到索引函数,而不是加载视图文件。并使用 Session flash data 来实现这一点。
喜欢,
控制器
$this->session->set_flashdata( 'error', 'Username and/or Password is incorrect' );
redirect('index');
要在 login_form.php 查看文件上打印消息,
查看 (login_form.php)
<?php if($this->session->flashdata('error')){
echo $this->session->flashdata('message');
}?>
你的控制器
public function validate_credentials() {
$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->index();
} else {
$this->load->model('user_model');
if ($this->user_model->checkUserAccess($this->input->post('username'))) { // checks if user has access to the site
if ($this->ldapAuth()) { // checks if successful authentication with LDAP server
$email = $this->user_model->getEmail($this->input->post('username'));
$gpcid = $this->user_model->getUserGPC($this->input->post('username'));
$this->user_model->updateTimestamp('users', 'DateLastLogin', 'UserID', $this->input->post('username'));
$data = array (
'username' => $this->input->post('username'),
'gpcid' => $gpcid,
'email' => $email,
'is_logged_in' => true
);
$this->session->set_flashdata($data);
redirect('profile');
} else {
$userdata = array (
'error' =>'Username and/or Password is incorrect',
);
$this->session->set_flashdata($userdata);
redirect('redirect url here'); // here is your url for index
}
} else {
$userdata = array (
'error' =>'You do not have access to the site',
);
$this->session->set_flashdata($userdata);
redirect('redirect url here'); // here is your url for index
}
}
}
控制器
public function loginaction() {
$this->load->library('session');
$a = $this->input->post('email');
$b = trim($this->input->post('password'));
$b1 = md5($b);
$c = 1;
$data = $this->userdata->userlogin($a, $b1, $c);
if($data) {
echo true;
foreach ($data as $login) {
$uid = $this->session->set_userdata('logId', $login->usr_id);
}
} else {
echo "Invalid username or password..!!!!";
$a = $this->input->post('email');
}
}