sooraj s pillai
sooraj s pillai

Reputation: 916

codeigniter checking session in all controller error

using the below code i used to check whether the user is logged in or not

if(!$this->session->userdata['logged_in'])
    {

    redirect("gt_login");
    }
    else
    {

    }

but this code is only working in localhost while i put this into server it shows error like this error

this is where i load session library

function __construct() { parent::__construct(); $this->load->library('session'); $this->load->database(); $this->load->helper('url'); $this->load->helper('form'); $this->load->model('gt_home_content_model'); if(!$this->session->userdata['logged_in']) { redirect("gt_login"); } else { } }

i am not much familiar to codeigniter and also is there any other method to do this can any one suggest me to do that ....

Upvotes: 2

Views: 1597

Answers (6)

Parvez Ahmed
Parvez Ahmed

Reputation: 650

set user_login_process look like this function


 public function userLoginCheck() {
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email', 'email', 'trim|required|xss_clean');
    $this->form_validation->set_rules('password', 'password', 'trim|required|xss_clean|callback_check_database');
    if ($this->form_validation->run() == FALSE) {
        $sdata = array();
        $sdata['error'] = '<font color="red">Invalid Email or Password </font>';
        $this->session->set_userdata($sdata);
        redirect("login_controller/index");
    } else {
            //Go to private area
        redirect('login_controller/userLogged', 'refresh');
    }
}

Upvotes: 0

sooraj s pillai
sooraj s pillai

Reputation: 916

this is the login controller that I'm using

public function user_login_process()
{

    $this->form_validation->set_rules('username', 'username', 'trim|required');
    $this->form_validation->set_rules('password', 'password', 'trim|required');


if ($this->form_validation->run() == false)
{

redirect('LoginController/index');
} 
else
{
    $data = array('username' => $this->input->post('username'),'password' => md5($this->input->post('password')));
    $result = $this->gt_login_model->login($data);
    if ($result == true)
    {
        $username = $this->input->post('username');
        $result = $this->gt_login_model->read_user_information($username);
        if ($result != false) 
        {
        $session_data = array('username' => $result[0]->user_name);
        // Add user data in session
        $this->session->set_userdata('logged_in', $session_data);
        $this->load->view('admin/intropage');
        }
    } 
    else
    {
    $data = array('error_message' => 'Invalid Username or Password');
    $this->load->view('admin/login/login_dashboard', $data);
    }
}
}

Upvotes: 0

Hikmat Sijapati
Hikmat Sijapati

Reputation: 6969

Try it like this...

Your problem is using [] for accessing session variables. [] is used for accessing array elements. A session variable can not be an array.

If you want to verify that a session value exists.Use $this->session->has_userdata('some_name');

if(!$this->session->has_userdata('logged_in')['username'])
{
  redirect("gt_login",'refresh');
}
else
{

}

Upvotes: 1

Parvez Ahmed
Parvez Ahmed

Reputation: 650

<?php 

after loding session library
$this->load->library('session');


you can set the session like this in login your login controller 

class Login_controller extends CI_Controller {
    public function __construct() {
        parent::__construct();
        $this->load->model('login_model');
    }

    public function userLogged() {
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $sdata=array();
            $sdata['name'] = $session_data['name'];
            $this->session->set_userdata($sdata);
            redirect('admin_controller/index');          
        } else {
            //If no session, redirect to login page
            redirect('login_controller/index', 'refresh');
        }
    }
}

now you can check user is logged in or not 
in another controller look like this code 

<?php  class Another_controller extends CI_Controller{
    public function __construct() {
        parent::__construct();
        if (!$this->session->userdata('logged_in')) {
            redirect('login_controller/index');
        }
    }
}

?>

Upvotes: 0

Adhan Timothy Younes
Adhan Timothy Younes

Reputation: 570

You must set your session data in your login controller after validation success. like:

$this->session->set_userdata('logged_in', TRUE);

and then you can call that in your construct function.

Upvotes: 0

Nishant Nair
Nishant Nair

Reputation: 1997

Please load the session library before use. In autoload.php

$autoload['libraries'] = array('session');

After that you can use the session methods in controller views and models.

$this->session->set_userdata('logged_in','Nishant');// set session data
$this->session->userdata('logged_in'); // using session data

Upvotes: 0

Related Questions