简体   繁体   中英

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.

还要检查您的 php.ini 文件是否有此选项:

session.auto_start=1

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']);

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 +

Have you loaded your session library

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

Or via autoload

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

i just solved my problem turns out i have old version of codeigniter 3 i upgrade my CI to the latest version available on the website . thank you all for the help

I experienced this problem with the $config['sess_driver'] set to 'database' .

I was using a valid MySQL user so CodeIgniter 3 was not complaining about the sql connection but the user did not have all the right permissions to read/write from the table php_sessions (or the one set by $config['sess_save_path'] ).

In my case, I was using the default user 'debian-sys-maint' in my database.php.

Your problem doesn't come from the session itself.

Try to:

  • Comment the redirect method and add a var_dump($user) instead to see if your $user is correctly set. The problem could come from your $user object which contains null values for id and name .

  • Change if(!$user == null){ by if ($user != null) { or if ($user) { .

If you are using PHP 7.1+/7.2 then this problem will happen. Change PHP version to down (to check if it works).

We also get same issues. our spec : InvoicePlane, PHP 7.0, code igniter v.3.1.11. Every controllers which use redirect() helper got err_connection_refused from browser.

After spend hours exhaustive debugging. We solved this by change IP Address base_url to domain name and set certificate from letsencrypt.

Hope this helps, I'm working with Homestead - Vagrant - php7.4 and the only way I got it working was following this answer on Github

Add this in your

Project/application/config/config.php

 /* 
 |-------------------------------------------------------------------------- 
 | Cookie Related Variables 
 |-------------------------------------------------------------------------- 
 | 
 | 'cookie_prefix'   = Set a cookie name prefix if you need to avoid collisions 
 | 'cookie_domain'   = Set to .your-domain.com for site-wide cookies 
 | 'cookie_path'     = Typically will be a forward slash 
 | 'cookie_secure'   = Cookie will only be set if a secure HTTPS connection exists. 
 | 'cookie_httponly' = Cookie will only be accessible via HTTP(S) (no javascript) 
 | 
 | Note: These settings (with the exception of 'cookie_prefix' and 
 |       'cookie_httponly') will also affect sessions. 
 | 
 */ 
 $config['cookie_prefix']   = ''; 
 $config['cookie_domain']   = ''; 
 $config['cookie_path']     = '/'; 
 $config['cookie_secure']   = FALSE; 
 $config['cookie_httponly']     = FALSE; 

also change lines from 289 to 352 in your

Project/system/core/Input.php

/** 
 * Set cookie 
 * 
 * Accepts an arbitrary number of parameters (up to 7) or an associative 
 * array in the first parameter containing all the values. 
 * 
 * @param   string|mixed[]  $name       Cookie name or an array containing parameters 
 * @param   string      $value      Cookie value 
 * @param   int     $expire     Cookie expiration time in seconds 
 * @param   string      $domain     Cookie domain (e.g.: '.yourdomain.com') 
 * @param   string      $path       Cookie path (default: '/') 
 * @param   string      $prefix     Cookie name prefix 
 * @param   bool        $secure     Whether to only transfer cookies via SSL 
 * @param   bool        $httponly   Whether to only makes the cookie accessible via HTTP (no javascript) 
 * @return  void 
 */ 
public function set_cookie($name, $value = '', $expire = 0, $domain = '', $path = '/', $prefix = '', $secure = NULL, $httponly = NULL) 
{ 
    if (is_array($name)) 
    { 
        // always leave 'name' in last place, as the loop will break otherwise, due to $$item 
        foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'httponly', 'name') as $item) 
        { 
            if (isset($name[$item])) 
            { 
                $$item = $name[$item]; 
            } 
        } 
    } 
  
    if ($prefix === '' && config_item('cookie_prefix') !== '') 
    { 
        $prefix = config_item('cookie_prefix'); 
    } 
  
    if ($domain == '' && config_item('cookie_domain') != '') 
    { 
        $domain = config_item('cookie_domain'); 
    } 
  
    if ($path === '/' && config_item('cookie_path') !== '/') 
    { 
        $path = config_item('cookie_path'); 
    } 
  
    $secure = ($secure === NULL && config_item('cookie_secure') !== NULL) 
        ? (bool) config_item('cookie_secure') 
        : (bool) $secure; 
  
    $httponly = ($httponly === NULL && config_item('cookie_httponly') !== NULL) 
        ? (bool) config_item('cookie_httponly') 
        : (bool) $httponly; 
  
    if ( ! is_numeric($expire) OR $expire < 0) 
    { 
        $expire = 1; 
    } 
    else 
    { 
        $expire = ($expire > 0) ? time() + $expire : 0; 
    } 
  
    setcookie($prefix.$name, $value, $expire, $path, $domain, $secure, $httponly); 
} 

Try this

$this->output->profiler(true);

To see if session is really set and also check if the library is loaded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM