GRY
GRY

Reputation: 724

Codeigniter Session->userdata is null

My project has several views which draw values out of the session->userdata array. the whole project works flawlessly on my development machine, but when I copied the project up to a server, I find that $this->session->userdata is null.

This is the code which inserts some values into the session->userdata array: It is called in the Controller.

    $sessionArray = array(
            'web_id' => $id,
            'paperId' => $paperId,
            'prelimId' => $prelimId,
            'intakeId' => $intakeId,
            'finalId' => $finalId,
     );

     $this->session->set_userdata($sessionArray);

In the view file I call

var_dump($this->session->userdata);

The value returned is null. As I mentioned before, the array is NOT null on my development computer (running WAMP). Does anyone know why?

Upvotes: 1

Views: 24300

Answers (2)

Hashem Qolami
Hashem Qolami

Reputation: 99484

In order to retrieve all session data as an ASSOC array, you could use the all_userdata() method. (Take a look at CodeIgniter user guide),

You can write that within your Controller and send the returned value to the view, as follows:

$data['userdata'] = $this->session->all_userdata();
$this->load->view('your/view.php', $data); // use $userdata in the view

Note: Using the session library directly inside the views may cause unexpected problems. Try managing your sessions in the Controller instead of the View.

Upvotes: 4

Venkata Krishna
Venkata Krishna

Reputation: 4305

Sorry for posting here in answer block first of all.......... Where you are assigning those values to $sessionArray. What i mean is in model or controller or in view file.

Are you calling var_dump($this->session->userdata) in view file.

Why don't you call that in controller and pass that as an array to view file via controller.

try like this in controller i will get user details in $login array from database for suppose when i submit username and password.

I will send those details to session array like this in model file

$this->session->set_userdata('logged_in_user', $login);

after that in controller if i want use user name from session array i will use like below.

$logged_in = $this->session->userdata("logged_in_user");
$model=array();
$model['logged_in_user']=$logged_in[0];

Once after i assigned to $model array i like to use that in view file so i will pass that $model array to view file from controller like this

$this->load->view('view_file_path',$model);

You can use that array in view file how ever you want from passed array.

Upvotes: 0

Related Questions