Codeigniter Sessions Not Working After Migration

Codeigniter Sessions not working after migration

There have been several issues reported for incompatibility of PHP version 7.1 and CI 3.1.6 not supporting $this->session->set_userdata('name', $name);

well, $this->session->set_userdata('name', $name); works, but the function userdata() accepts only one argument and expects it to be a string

if you look into session library (/system/libraries/Session/Session.php), you'll find near row

747:

/**
* Userdata (fetch)
*
* Legacy CI_Session compatibility method
*
* @param string $key Session data key
* @return mixed Session data value or NULL if not found
*/
public function userdata($key = NULL)
{
if (isset($key))
{
return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
}
elseif (empty($_SESSION))
{
return array();
}

$userdata = array();
$_exclude = array_merge(
array('__ci_vars'),
$this->get_flash_keys(),
$this->get_temp_keys()
);

foreach (array_keys($_SESSION) as $key)
{
if ( ! in_array($key, $_exclude, TRUE))
{
$userdata[$key] = $_SESSION[$key];
}
}

return $userdata;
}

but alternatively you can fetch an array like $name=array('firstname'=>'mr smith') with native $_SESSION like this:

set:

$_SESSION['name']=$name; 

or

$this->session->set_userdata('name', $name);

get:

echo $_SESSION['name']['firstname']; //etc..

Codeigniter session stored in database but not working

Actually, it was a problem with Codeigniter 3.1.6 and PHP 7.1.7. I've just upgraded CI to newest version.

codeigniter session not working properly

Some changes needs to be done in config file.

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = sys_get_temp_dir();
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

This issue is likely to be faced post CI migration.
Make sure model name is declared correctly like

class Name extends CI_Model { }

First letter must be upper case in CI 3. save file in same name.

Another reason is sess_save_path. give correct path that should resolve this error

Codeigniter Session Not Holding Values Across Controllers

I've found a solution to your problem. This issue is common to your CI version.

all you need to do is replace the system folder of your Codeigniter app to a newer system version. and you are all set.

it fix this issue using :

CI_Version: 3.1.2; 3.1.3; 3.1.4 up+

and i'm using the latest version now. 3.1.10.

Codeigniter 3.0.5 Session is not working in online

The issue raised because the difference in php in local and with production server.It could corrected by changing the php version of cpanel matching with local which had initially coded



Related Topics



Leave a reply



Submit