user3574766
user3574766

Reputation: 631

CodeIgniter Session Data not working

I'm creating a session at login but it's not saving the variables I store inside the session nor is it carrying them across to other pages:

Controller code:

    function CheckDatabase($password) //This function is only run when password validation is correct.//
{
    $username = $this->input->post('Username'); //Sets the username as a $username.//
    $result = $this->User_model->Login($username, $password);

    if($result)
    {
        $sess_array = array();
        foreach($result as $row)
        {
            $sess_array = array( //Makes an array of the data to be stored in the session.//
            'UserID' => $row->UserID,
            'Username' => $row->Username
            );

            $this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.//
        }

        return TRUE;
   }

    else //Ran if the username or password aren't matched in the CIUsers database. Returns error message.//
    {
        $this->form_validation->set_message('CheckDatabase', 'Invalid login details.');
        return false;
    }
}

Index function on the same controller (which should stop a user going back to the login screen but doesn't)

    function index() //Default function that is run when this controller is called.//
{

    if($this->session->userdata('logged_in'))
    {
        redirect('Home_controller');
    }

    else
    {
        $this->load->helper(array('form'));
        $this->load->view('Login_view');
    }

}

Code on the home view (which browser is directed to once logged in)

<?php echo $UserID .": ".$Username; ?>

I get this error for displaying the session data:

Message: Undefined variable: Username

Filename: views/Home_view.php

Line Number: 9

Upvotes: 3

Views: 1743

Answers (4)

K Ravi
K Ravi

Reputation: 869

You have to retrieve it from session like this before using:

$user = $this->session->userdata('logged_in');
$Username = $user['Username'];
$UserID = $user['UserID'];

Upvotes: 2

B.Nadesh kumar
B.Nadesh kumar

Reputation: 226

 if($result)
{
    $sess_array = array();
    foreach($result as $row)
    {
        $sess_array = array( //Makes an array of the data to be stored in the session.//
        'UserID' => $row[0]->UserID,
        'Username' => $row[0]->Username
        );

        $this->session->set_userdata('logged_in', $sess_array); //Sets $sess_array as the session.//
    }

    return TRUE;

}

Upvotes: 0

Ricky
Ricky

Reputation: 568

Try like this

$newdata = array(
                    'user_id' => $rows->id,
                    'user_name' => $rows->name,
                    'user_image' => $rows->user_image,
                    'user_email' => $rows->email,
                    'logged_in' => TRUE,  
                    'user_type' => $rows->user_type
                );
            }
            $this->session->set_userdata($newdata);

And check in your controller construct for user session

<?php
    if (!defined('BASEPATH'))
        exit('No direct script access allowed');
    class User extends CI_Controller {
            public function __construct() {
            parent::__construct();

        //Here we will check for the session value if it is true then the function will run else it will redirect to login page

            if ($this->session->userdata('logged_in') == FALSE) {
                redirect(site_url('your login page'));
            } else {
              // if there is session value
              redirect(site_url('User/your function name');
            }
    }
}
    ?>

For complete explaination read this tutorial

http://w3code.in/2015/10/session-handling-in-codeigniter/

Upvotes: 0

Yuva Raj
Yuva Raj

Reputation: 3881

To retrieve the session variable, you have to do like this

 $username = $this->session->userdata['logged_in']['Username'];
 echo $username;

Upvotes: 2

Related Questions