2

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 ....

5
  • try now it will works. Commented Jan 18, 2017 at 4:34
  • if(!$this->session->has_userdata['logged_in']['username']) try hope it will works. Commented Jan 18, 2017 at 5:56
  • see edited answer. Commented Jan 18, 2017 at 5:58
  • Hi, still need help for this ? Commented Dec 21, 2017 at 8:11
  • no i solved this. Commented Dec 21, 2017 at 8:17

6 Answers 6

1

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
{

}
Sign up to request clarification or add additional context in comments.

7 Comments

[] is used for accessing array elements.
did you load session library.
where you set logged_in session variable?
@soorajspillai: You should add that code to your question.
@Hikmat : i didnt get that
|
0

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

4 Comments

yes i already set all these and also i use session by these methods
Are you still facing the issue? you were using [] to access session variable. It should be () as its and function.
yes i'm still stuck with this problem , now i'm using () to access session variable
use if($this->session->userdata('logged_in') == '' )
0

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.

1 Comment

just load session library in your login controller
0
<?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');
        }
    }
}

?>

1 Comment

i posted my login controller can you check it please
0

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);
    }
}
}

Comments

0
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');
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.