1
votes

I set session as an autoload in config/autoload.php

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

and config session and encrypt in config/config.php

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'dornika';
$config['sess_expiration'] = 86400;
$config['sess_save_path'] = NULL;
$config['sess_match_ip'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = TRUE;  

and then I set session data in an array with

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

and load this in controller with

$this->session->userdata('login');

which contain true but in my controller it becomes null instead of 1(or true)

here is code in model

$is_exist = $this->db->count_all_results();

        if($is_exist > 0){
         $data_session = array(
             'username' => $_POST['username'],
             'login' => true
         );
            $this->session->set_userdata($data_session);
            redirect('dashboard/index');
        }else{
            redirect('login');
        }

and controller

<?php
defined('BASEPATH') OR exit('No direct script access allowed');


class Dashboard extends CI_Controller
{

    public function index()
    {
       $login = $this->session->userdata('login');
        if ($login == true) {

            $data['title'] = 'dashboard';
            $this->template->load('panel/dashboard', $data);

        }else{
            redirect('login');
        }
    }
}
1

1 Answers

0
votes

You're trying to access $this->session->userdata('login') which doesn't there, you haven't given any name to your session. You can solve this by giving name to session while setting it.

Code

# Setting Session
$this->session->set_userdata("login", $array);    

# Accessing Session
$user_data = $this->session->userdata("login");
echo $user_data['some_key']; // or var_dump($user_data);