I am facing session related issues in my running project. So I thought to take a fresh copy and checking sessions with the simple code. So I downloaded the fresh copy of Codeigniter 2.1.3 and added session library in auto load. I also set the 5 digit encryption key in config file. Then I just created the following 2 methods in welcome controller:
public function gettingSess() {
echo '<pre>';
print_r($this->session->all_userdata());
echo '</pre>';
exit;
}
public function settingSess() {
$arr = array('start' => '456');
for($i=0;$i <= 999;$i++) {
$arr['lets_test' . $i] = $i * 100;
}
$this->session->set_userdata('sessVar', $arr);
echo '<pre>';
print_r($this->session->all_userdata());
echo '</pre>';
exit;
}
Then first I run the http://mydomain.com/ci/index.php/welcome/settingSess and its output is as follow, which is perfect:
Array (
[session_id] => dd0a684ab53e937309ee9fdbcf24468f
[ip_address] => 127.0.0.1
[user_agent] => Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0
[last_activity] => 1371458835
[user_data] =>
[sessVar] => Array
(
[start] => 456
[lets_test0] => 0
[lets_test1] => 100
.
.
[lets_test999] => 99900
)
)
Then I run the http://mydomain.com/ci/index.php/welcome/gettingSess and its output is as follow:
Array(
[session_id] => dd0a684ab53e937309ee9fdbcf24468f
[ip_address] => 127.0.0.1
[user_agent] => Mozilla/5.0 (Windows NT 6.1; rv:21.0) Gecko/20100101 Firefox/21.0
[last_activity] => 1371458835
[user_data] =>
)
Please suggest me what I am doing wrong and what is the issue, why I am not getting the session variables in gettingSess method.
Thanks.
I am not completely sure if this will help, however, I had a session related issue aswell regarding running sessions across multiple files. I had to contact the hostprovider and ask them to run a specific code: You might want to check this thread out: PHP File Cannot Recover Session Variable Across Pages
may be your session is not setting up properly. so better you create a function then set your session then get the session. try code like below
public function demo()
{
$this->settingSess();
$this->gettingSess();
}
and in you gettingSess() function add a line like below
public function gettingSess()
{
print_r($this->session->all_userdata());
echo $this->session->userdata('sessVar');
exit;
}
Try this:
$this->session->set_userdata($arr);
set_userdata
only accepts two elements if they are of 'key', 'value' type - where the second element must be a string (no arrays), e.g. $this->session->set_userdata('some_name', 'some_value');
- see the manual.
Set $config['cookie_secure'] = FALSE;
in config/autoload.php.
If set true, cookies will only be set if a secure connection exists i.e. when website is running on HTTPS.
Session data is stored in the user's cookie. Cookies can only hold 4KB of data, so be careful not to exceed the capacity.Try setting a single session value
$this->session->set_userdata('some_name', 'some_value');