function login(){
$data = array('username' => $_POST['username'], 'password' => $_POST['password']);
$this->form_validation->set_rules('username', 'Username', 'required|callback_username_check');
$this->form_validation->set_rules('password', 'Password', 'required');
if($this->form_validation->run() == FALSE){
$this->load->view('Login');
} else {
$result = $this->LoginModel->account_check($data);
if($result){
if($result['status'] = 'admin'){
$isLoggedin = array('user_type' => 'administrator' ,'admin_name' => $result['username'] , 'is_loggedin' => TRUE);
$this->session->set_userdata($isLoggedin);
$this->load->view('admin/homepage');
} else if($result['status'] = 'user'){
$isLoggedin = array( 'user_name' => $row['username'] , 'user_type' => 'user' , 'is_loggedin' => TRUE);
$this->session->set_user($isLoggedin);
$this->load->view('user/homepage');
}
}
}
}
function username_check($username){
$result = $this->LoginModel->username_check($username);
if($result != TRUE){
$this->form_validation->set_message('username_check' , 'Username does not exist');
return FALSE;
} else return TRUE;
}
MODEL:
function username_check($username){
$query = $this->db->get_where( 'admin' , array('username' => $username))->result_array();
if(!empty($query)){
return TRUE;
}else{
$query = $this->db->get_where( 'user_mst', array('username' => $username))->result_array();
if(!empty($query)){
return TRUE;
}else return FALSE;
}
}
function account_check($data){
$query = $this->db->get_where( 'admin' , array( 'username' => $data['username'] , 'password' => $data['password'] ))->result_array();
if(!empty($query)){
foreach($query as $row){
$result['status'] = 'admin';
$result['username'] = $row['username'];
$result['password'] = $row['password'];
return $result;
}
} else{
$query = $this->db->get_where( 'user_mst' , array( 'username' => $data['username'] , 'password' => $data['password'] ))->result_array();
if(!empty($query)){
foreach($query as $row){
$result['status'] = 'user';
$result['username'] = $row['username'];
$result['password'] = $row['password'];
return $result;
}
}
}
}
How do i access the user data, im going to use this to run an if per view if an account is logged in and if it is an admin acc.
I would like to read some opinion regarding how im using the session if i am 100% correct.
Also, i have a question,
This code <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); ?>
blocks direct access to the view, prevents view access but not specifically checks if a user is logged in.
How can i utilize this with the session. If its redundant please tell me,
Codeigniter passes data to the view with a second parameter:
$this->load->view('user/homepage');
doesn't pass any data.
$this->load->view('user/homepage', $data);
Would pass in a $data variable that you could use. ( https://ellislab.com/codeigniter/user-guide/general/views.html )
Another point - I'd consider using the redirect() function after a successful login rather than loading the view you want in the login method. Use the URL Helper to do this ( https://ellislab.com/codeigniter/user-guide/helpers/url_helper.html ).
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.