Welcome Guest, Not a member yet? Register   Sign In
Session issue
#1

(This post was last modified: 12-14-2017, 08:58 AM by yinkoh.)

Hello,

I'm getting confused with a session issue...

I'm using a parent controller (MY_Controller) for all others controllers. Each of them is correctly initiated with parent::__construct() and everything is going well.

Right now, I'm doing the backoffice part of my site and so, I need to store a flag for identified users.

PHP Code:
class MY_Controller extends CI_Controller
{
 public function 
__construct()
 {
 
 parent::__construct();
 
 $this->load->library->('session');
 
 }

PHP Code:
class Backoffice extends MY_Controller
{
 public function 
__construct()
 {
 
 parent::__construct();
 
 }

 public function 
index()
 {
 
 if ( ! $this->session->has_userdata('is_logged') )
 
   $this->load->view('login');
 
 else
    $this
->load->view('admin');
 
 }

 public function 
login()
 {
 
 if $this->input->post('user') == 'user' && $this->input->post('passwd') == 'passwd' )
 
  $this->session->set_userdata('is_logged' => 1);
 
 }


My problem is that session always gets empty unless I do not use the CI Session library and therefore uses $_SESSION from native php.

I just dont get what part is wrong in my code.


Ps: I've tried using both files or database driver in application/config/config.php but without any changes.
Reply
#2

(This post was last modified: 12-15-2017, 06:55 AM by dave friend.)

In the code you show this is the problem

PHP Code:
$this->session->set_userdata('is_logged' => 1); 

Try this instead

PHP Code:
$this->session->set_userdata('is_logged'1); 

Or this if you really want to use an array

PHP Code:
$this->session->set_userdata(['is_logged' => 1]); 

If you are using a Codeigniter version > 3 then you can use $_SESSION directly. If you read the documentaion
carefully you will see that is recommended.

PHP Code:
$_SESSION['is_logged'] = 1

To check for session data I like this approach

PHP Code:
public function index()
{
     if ($this->session->is_logged == 1)
    
        $this->load->view('admin');
    }
    else
   {
       $this->load->view('login');
   }


If you are certain that session data was set but it isn't showing up in index() then you probably have an improper session and/or cookie configuration.
Reply
#3

You're right about the code mistake but in the actual code, i set the proper way to store the data, i just write it down wrongly.

I tried your code just in case but I still get the same problem.

I'm gonna try to check it on a new install of the lastest CI to see if it's not a config file related issue.
Reply
#4

Did you try to autoload the session?

./application/config/autoload.php

Makes no since adding it to a MY_Controller if your going to use it all the time.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

Quote:Makes no since adding it to a MY_Controller if your going to use it all the time.

Except perhaps unless MY_Controller is always used.
Reply
#6

Well, starting of a fresh new install of ci solved the trouble...

I still dont get it but at least i can keep on coding Smile

Thanks everyone.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB