Redirecting codeigniter name too much to home controller
I have one problem with my login and views, I want after the user logs in and logs out of the session and then redirects me to my home, but the user slips the url for the root project or the login url, then it should stay at home. my code looks like this
MY_Controller
protected function isLogged(){
if (!$this->session->userdata('log'))
redirect('login');
}
To come in
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends MY_Controller {
public function __construct(){
parent::__construct();
}
public function index(){
$data['module'] = 'Login';
$this->load->view('header',$data);
$this->load->view('login');
}
public function getAccess(){
$username = $this->input->post('username', TRUE);
$password = $this->input->post('password', TRUE);
$result = $this->user->login($username,$password);
if (!empty($this->input->post('username')) && !empty($this->input->post('password'))) {
if (!$result) {
$this->json(array('error' => 'invalid username or password'));
}else{
$data_session = array(
'id' => $result['id'],
'first_name' => $result['first_name'],
'last_name' => $result['last_name'],
'type' => $result['profile_id'],
'logged_in' => TRUE
);
$this->session->set_userdata('log',$data_session);
}
} else {
$this->json(array('empty' => 'You did not fill out the required fields.'));
}
}
public function logout(){
$this->session->sess_destroy();
redirect('login','refresh');
}
}
home controller
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Home extends MY_Controller {
public function __construct(){
parent::__construct();
$this->isLogged();
}
public function index(){
$data = $this->session->userdata('log');
$data['module'] = "Home";
$data['fields'] = $this->getModules();
$this->load->view('header',$data);
$this->load->view('index');
$this->load->view('home');
$this->load->view('footer');
}
}
/* End of file Welcome.php */
/* Location: ./application/controllers/Welcome.php */
+3
source to share
2 answers
You can see how this code can be helpful
This is my login controller
defined('BASEPATH') OR exit('No direct script access allowed');
class Login extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('url','form','file'));
$this->load->model(array('admin_model'));
//print_r($_SESSION);
// print_r($data['all_cats']);die();
}
public function index()
{
if($this->input->post('submit')!='')
{
$email = $this->input->post('email');
$pwd = $this->input->post('password');
$login = $this->admin_model->login($email,$pwd);
if(!empty($login))
{
redirect('Welcome/dashboard');
}
else
{
$this->session->set_flashdata('error', 'Incorrect Login Details');
$this->load->view('login');
}
}
else
{
$auth = checkAdminLogin();
if( $auth == 1)
{
redirect('Welcome/dashboard');
}
else
{
$this->load->view('login');
}
}
}
}
And what's my controller hello
defined('BASEPATH') OR exit('No direct script access allowed');
class Welcome extends CI_Controller {
function __construct()
{
parent::__construct();
$this->load->helper(array('url','form','file'));
$this->load->model(array('admin_model'));
$data['dbname']=$this->admin_model->get_db_name();
$data['all_cats']=$this->admin_model->get_categories();
$data['websites']=$this->admin_model->website_type();
$data['sub_cats']=$this->admin_model->get_sub_cats();
$admin_id=$this->session->userdata('adminId');
if($admin_id == '')
{
redirect(base_url());
}
$this->load->vars($data);
//print_r($_SESSION);
// print_r($data['all_cats']);die();
}
// Admin Login Page
// End Function
// Redirect to Admin DashBoard
public function dashboard()
{
$auth = checkAdminLogin();
if( $auth == 1)
{
$data = array();
$this->load->view('index',$data);
}
else
{
redirect('Welcome');
}
}
// End Function
// Logout Function
public function logout()
{
$this->session->unset_userdata('adminId');
$this->session->sess_destroy();
redirect('Login');
}
// End Function
// Manage Blog Categories
public function blog_categories($reload)
{
$data['list']=$this->admin_model->blog_categories();
if(isset($reload) && $reload !='')
{
$this->load->view('datatable',$data);
}
else{
//print_r($data['list']);die();
$this->load->view('users-list',$data);
}
}
+1
source to share