Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php - Session are lost after redirect in CodeIgniter 3

I would like to have some help in CodeIgniter 3. Every time I login and redirect to the index page, session is lost.

Here is my code:

Controller:

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

class Secretariat extends CI_Controller {
    public function __construct(){
        parent::__construct();
        $this->load->helper(array('form', 'url'));
        $this->load->model('SecretariatModel');
        $this->load->model('IndiRegModel');
        $this->load->model('RoomModel');
        $this->load->model('BuildingModel');
        $this->load->model('BilletModel');
        $this->load->model('BatchRegModel');
        $this->load->library('form_validation');
        $this->load->library('session');
    }

    public function secretariatLogin(){

        if ($this->session->isSecretariat){
            redirect('secretariat/index', $data);


        } else{

            $data['title'] = 'PACSA - Philippine Association of Campus Student Adviser';

            $this->load->view('include/toppage', $data);
            $this->load->view('include/defaultnavbar', $data);
            $this->load->view('pacsa/slider');
            $this->load->view('secretariat/secretariatLogin', $data);
            $this->load->view('include/bottompage');

       }

    }

    public function signin(){
        $secretariat = array(
            'sec_email' => $this->input->post('sec_email'),
            'sec_password' => sha1($this->input->post('sec_password'))
        );

        $user = $this->SecretariatModel->getSecretariat($secretariat);
        //print_r($user->name);die();

        if(!$user == null){

            $newdata = array(
                'sec_id' => $user->sec_id,
                'sec_name'  => $user->sec_name,
                'sec_lastname' => $user->sec_lastname,
                'sec_email' => $user->sec_email,
                'sec_password' => $user->sec_password,
                'sec_status' => $user->sec_status,
                'sec_address' => $user->sec_address,
                'logged_in' => TRUE,
                'isSecretariat' => TRUE
            );

            $this->session->set_userdata($newdata);            
            redirect('secretariat/index');
        } else {
            $data['title'] = 'PACSA - Philippine Association of Campus Student Adviser';
            $data['message'] = 'Invalid email or password';

            $this->load->view('include/toppage', $data);
            $this->load->view('include/defaultnavbar', $data);
            $this->load->view('pacsa/slider');
            $this->load->view('secretariat/secretariatLogin', $data);
            $this->load->view('include/bottompage');
        }
    }    

    public function index(){
        $data['title'] = 'PACSA - Philippine Association of Campus Student Adviser';
        $id = $this->session->sec_id;
        var_dump($id);
        echo die();

        $this->load->view('include/toppage', $data);
        $this->load->view('include/secretariatnavbar', $data);
        $this->load->view('pacsa/slider');
        $this->load->view('secretariat/index', $data);
        $this->load->view('include/bottompage');
    }
}

So after redirecting to the index page, I want to verify if there is a session involved. I tried to echo the id and the name of the user but I get a null value.

like image 588
Kyle Cipriano Avatar asked Jan 28 '18 10:01

Kyle Cipriano


4 Answers

Go to

system/libraries/Session/session.php

at Line no 281 and replace

ini_set('session.name', $params['cookie_name']); 

by

ini_set('session.id', $params['cookie_name']);

This problem occurs normally while upgrading PHP later version to 7.3 +

like image 114
Nikunj Dhimar Avatar answered Nov 15 '22 20:11

Nikunj Dhimar


also check your php.ini file for this option:

session.auto_start=1
like image 38
Sofyan Thayf Avatar answered Nov 15 '22 18:11

Sofyan Thayf


in this link:

https://php.developreference.com/article/23484402/Codeigniter+session+data+lost+after+redirect

if you are working in CI 3.x and just upgraded your server php version to php 7.x Go to system/libraries/Session/session.php at Line no 281 and replace ini_set('session.name', $params['cookie_name']); by ini_set('session.id', $params['cookie_name']);

like image 24
guest Avatar answered Nov 15 '22 20:11

guest


Have you loaded your session library

this->load->library('session')

Or via autoload

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

like image 21
Ntiyiso Rikhotso Avatar answered Nov 15 '22 19:11

Ntiyiso Rikhotso