I have been facing this issue since yesterday and yet I could not resolve this. Issue is sessions are not working in local environment but when for a testing purpose I put the same files on a live server, they work all okay.
Here if my config.php file:
$config['sess_driver'] = 'files'; $config['sess_cookie_name'] = 'ci_session'; $config['sess_expiration'] = 7200; $config['sess_save_path'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_time_to_update'] = 300; $config['sess_regenerate_destroy'] = FALSE; $config['cookie_prefix'] = ''; $config['cookie_domain'] = ''; $config['cookie_path'] = '/'; $config['cookie_secure'] = FALSE; $config['cookie_httponly'] = FALSE;
Here is how I save the data in one of my model files
$this->session->set_userdata('user',$result); //$result works fine, it produces right result
In my view, I tried to access this by:
$this->session->userdata['user']['name']; //name here is an element in result array
I get this error:
Severity: Notice
Message: Undefined index: user
Filename: home/home.php
Line Number: 2
To my surprise, this same code runs without any error on server.
Also, to be able to know the data being saved by Codeigniter sessions, I tried database method.
I changed the code to below in config.php file
$config['sess_driver'] = 'database'; $config['sess_cookie_name'] = 'ci_sessions'; $config['sess_expiration'] = 7200; $config['sess_save_path'] = 'ci_sessions'; $config['sess_match_ip'] = FALSE; $config['sess_time_to_update'] = 300;
Instead of inserting one row of data, it inserts four. Please see below screenshot from the database (last four rows).
Can anybody point to the error? Any help will be appreciated.
5 Answers
Answers 1
Your question has a few parts, I'll answer each in turn.
Regarding retrieving values from the session
userdata()
is a method. The way you are accessing it treats it as an array:
$this->session->userdata['user']['name']; // Bad
As the docs describe, you should pass the key of the value you want to retrieve as a parameter to the userdata
method:
$user = $this->session->userdata('user'); // Now you can echo $user['name'];
Even simpler is to use the newer syntax:
$user = $this->session->user; // Now you can echo $user['name'];
Regarding the 4 session records in the database
Check the timestamp
fields. They show 3 different times, meaning those are 3 separate events on the site. The first occurs at 1501911275
, then 6 seconds later there are another 2 records, and then 139 seconds later the last record. Those represent 3 separate requests to the site - probably you were visiting different pages, or testing and reloading.
As to why there are multiple records, that's because the session ID is periodically regenerated. Exactly how often that happens depends on your configuration see the sess_time_to_update
config option. How often they are destroyed also depends on your config (see sess_regenerate_destroy
in the same section of the docs) - but normally older session records stick around for a while and are cleaned up later by garbage collection. So while you have only 1 "current" session record, old records can persist for a while.
Regarding your sess_save_path
From the wording of your question I think you want to use the files
session driver, and only tried the database
driver for testing. If that's correct, there is a problem in your files session configuration which you should fix.
only absolute paths are supported for $config['sess_save_path'].
The comments in config/config.php
also say:
WARNING: Only absolute paths are supported!
Your configuration specifies that session data should be saved in ci_sessions
, which is not an absolute path:
$config['sess_save_path'] = 'ci_sessions'; // Bad
I am not sure how will be interpreted, but my guess is the directory won't exist, and the web server will not have permissions to create it. I can't imagine why it works on your live server.
Change the path to an absolute path, and make sure your web server can write to it. Again from the docs
mkdir /<path to your application directory>/sessions/ chmod 0700 /<path to your application directory>/sessions/ chown www-data /<path to your application directory>/sessions/
(change www-data
to the user your web server is running as). And then:
$config['sess_save_path'] = '/<path to your application directory>/sessions/';
Answers 2
If you read the Codeigniter Class Reference setuserdata
expects the second argument to be a value, that in case first argument is a key, hence the error.
with the Codeigniter way, you won't be able to achieve what you want as it uses:
set like this:
$result=array('name'=>'john doe');
$this->session->set_userdata($result);
or $this->session->set_userdata('name','john doe');
get like this:
echo $this->session->userdata(['name']);
on the other hand using vanilla php $_SESSION
you can achieve it like so:
set: $_SESSION['user']=$result;
get: echo $_SESSION['user']['name'];
see about $_SESSION at php manual
note:
To my surprise, this same code runs without any error on server.
this is due to your ENVIRONMENT
setting, as you have error_reporting
limited at the production (live) server. This is set in your root index.php
.
change define('ENVIRONMENT', isset($_SERVER['CI_ENV']) ? $_SERVER['CI_ENV'] : 'development');
to: define('ENVIRONMENT', 'production');
and you won't see the error on your localhost as well.
update:
Actually $this->session->set_userdata('name', $name);
works, but the function userdata() only accepts one argument and expects it to be a string
if you look into session library (/system/libraries/Session/Session.php), you'll find it 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; }
Answers 3
Follow these
In config.php
Set $config['sess_save_path'] = NULL;
As you said $this->session->set_userdata('user',$result); //$result works fine, it produces
above $result
shoulb be like this
$result = array( 'user' => 'Ab', ..... 'logged_in' => TRUE );
How To Access session key
echo $this->session->userdata('user');
or
$user = $this->session->userdata('user'); echo $user;
Answers 4
Seems the issue was with PHP version 7.1 as CI 3.1.6 was not supporting that PHP version.
I'm not sure whether this is the correct explanation but when I switched back to PHP version 5.6, everything started working absolutely fine.
Answers 5
I think issue with access of session data with incorrect syntax.you can use below syntax for set session data and get session data.
Example for set session data
$session_data = array('name' => "kiran"); $this->session->set_userdata('user', $session_data);
Example for get stored session data
$this->session->userdata('user')['name'];
0 comments:
Post a Comment