Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Codeigniter session is not working on PHP 7

Tags:

I am using codeigniter version 3.0.6 on PHP 5.6 and it works fine. but when I run same project on PHP 7.1 , codeigniter session is not working. I set session like below

$login_session = $this->session->set_userdata("user_session",$session_data); 

when I print $login_session it is blank on PHP 7

like image 727
hrishi Avatar asked Aug 18 '17 12:08

hrishi


2 Answers

Recently I've faced the same problem by upgrading my php version from 5.6 to 7.1 My development environment is XAMPP (Apache + MariaDB) using php7.1.4

Please follow the below steps:

1) Go to system/libraries/Session/Session.php

2) Comment session_start() by adding //.

3) Go down to line around 312-315 where it says Security is king, and comment out the following lines:

ini_set('session.use_trans_sid', 0); ini_set('session.use_strict_mode', 1); ini_set('session.use_cookies', 1); ini_set('session.use_only_cookies', 1); ini_set('session.hash_function', 1); ini_set('session.hash_bits_per_character', 4); 

4) Then go to your main index.php ( the root index.php )

5) Add session_start() at the top once.

It works for me.

like image 75
Shafiq Avatar answered Sep 22 '22 19:09

Shafiq


Session id is different in php 7.1 and not pass the regex checking.

A simple fix: In system/libraries/Session/Session.php arround line 133

OR ! preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']]) 

remove '{40}$'

OR ! preg_match('/^[0-9a-f]/', $_COOKIE[$this->_config['cookie_name']]) 

The problem is that the session id is deleted from cookie because the regex mismatch. So the session id always renew.

This can fix the regular expression and prevent it run the line "unset($_COOKIE[$this->_config['cookie_name']]);"

It totally works for me !

FYI. Find this after I fix it myself. https://github.com/bcit-ci/CodeIgniter/issues/4830

like image 25
Leon Lo Avatar answered Sep 18 '22 19:09

Leon Lo