I am getting error while storing sessions into db in codeigniter3 - session

After login i am getting the error:
Message: ini_set(): A session is active. You cannot change the session
module's ini settings at this time
$config['sess_driver'] = 'database'; #files, database
$config['sess_cookie_name'] = 'ci_sessions';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';//its your table name, files=NULL
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
this is my config file.

Related

CodeIgniter session is empty

I set a session in a class from libraries. Then I tried to get the value of this session in another class from libraries. The value is empty.
I took a look in CodeIgniter Library to find out what the problem could be and at the some similar topics from StackOverflow but I don't see anything wrong from my end. maybe I am missing something. Please help.
// IN THIS CLASS I SET THE SESSION 'fr_phone'
class Form_submit_fr {
var $CI;
protected $fr_id;
protected $fr_data = array();
public function __construct() {
$this->CI =& get_instance();
if (strpos($this->CI->uri->segment(1),'frid') !== false){
$this->fr_id = (int)str_replace('frid','',$this->CI->uri->segment(1));
$this->fr_data = $this->get_fr_data();
// add the phone to session
if (isset($this->fr_data->city_toll) && $this->fr_data->city_toll != '') {
$fr_phone = $this->fr_data->city_toll;
} else {
$fr_phone = $this->fr_data->city_phone;
}
// here the session is set
$this->CI->session->set_userdata('fr_phone', $fr_phone);
//the result is correct
echo $this->CI->session->userdata('fr_phone') . ' - fr_phone<br>';
}
}
....
}
// IN THIS CLASS I NEED TO GET THIS SESSION 'fr_phone'
class P_details {
protected $page_link;
protected $fr_data;
var $CI;
public function __construct(){
$this->CI =& get_instance();
if ($this->CI->uri->segment(2) == false && $this->CI->uri->segment(3) == false) {
$this->page_link = $this->uri->segment(1)."/";
} else if ($this->CI->uri->segment(1) != false && $this->CI->uri->segment(2) != false && $this->CI->uri->segment(3) == false) {
$this->page_link = $this->CI->uri->segment(1)."/".$this->CI->uri->segment(2)."/";
} else if ($this->CI->uri->segment(1) != false && $this->CI->uri->segment(2) != false && $this->CI->uri->segment(3) != false) {
$this->page_link = $this->CI->uri->segment(1)."/".$this->CI->uri->segment(2)."/";
$this->fr_data = $this->get_fr_data($this->CI->uri->segment(3));
}
$this->CI->load->model('getDetails_model');
}
public function get_p_id($type = '') {
$details_array = $this->CI->getDetails_model->all_details($this->page_link);
if (empty($details_array)) {
return FALSE;
}
else {
$data['fr_phone'] = '';
$data['detail'] = $details_array;
// get phone from session
// here the session is empty
echo $this->CI->session->userdata('fr_phone') . ' - fr_phone<br>';
if ($this->CI->session->userdata('fr_phone')) {
$data['fr_phone'] = $this->CI->session->userdata('fr_phone');
}
if ($this->fr_data != '') {
$data['fr_detail'] = $this->fr_data;
}
return $data;
}
}
...
}
this is my config file:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = NULL;
$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;
this is my autoload file:
$autoload['libraries'] = array('database','session');
I have made the suggested changes in config file for $config['sess_save_path']. thanks a lot. I could see the files in the writable folder and the entries in database when I checked this way as well:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 43200;
$config['sess_save_path'] = APPPATH.'writable';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Also I used database for session:
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 43200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
In both cases the session works if I define a session in Controller in constructor or index function. I can get session in View files and in other Classes but if I set a session under a condition for a specific link then this session is always empty when I want to get it in a class from libraries folder or in view files:
//in controller
if (strpos($this->uri->segment(1),'frid') !== false){
...
$fr_phone = '111-222-3333';
$this->session->set_userdata('fr_phone', $fr_phone);
echo $this->session->userdata('fr_phone') // works
}
When I call this session in a Class from libraries folder, the session is empty: above example for class P_details. And there is no way to get a session that is set in a Class from libraries folder and to get it in Another Class from libraries folder. It is very strange and I cannot get it. Also I tried to get session in a different controller and no success.
i had similar issue with one project. After long try i fix the issue the following way
1) in config.php
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'writable absolute path';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
and created the folder named "writable absolute path" in the root
Hopes this will help
It is very strange but I removed the session from autoload:
$autoload['libraries'] = array('database');
I added session_start(); in controllers and I set the session in a common way:
$_SESSION['fr_phone'] = $fr_phone;
Then I was able to get this value in any files. Is this a bug?
I tried without session_start() by adding $this->load->library('session'); and it did not work.
Can anyone find an answer to this issue?
I am using CodeIgniter 3.0.2

How can we get current controller name in config file of codeigniter

I am working in codeigniter, i have some config variables in config.php file of codeigniter, but for welcome controller i want to changes its config variable, i am not able to get controller name in config file of codeigniter, i am using this function $controller = $this->router->fetch_class();, but it is not working can anyone please help me how can we get it ? I have below config variables, but i want to only enable it if controller name is not a welcome
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 900; //7200; 900
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 0; //
$config['sess_time_to_update'] = $config['sess_expiration'];

Code igniter session_start not working

I have a codeigniter site , and for the past day it encountered a problem without any code modifications . It says :
A PHP Error was encountered
Severity: Warning
Message: session_start(): open(/tmp/sess_5f9c0f8499f8970f68b2d94c92621af6, O_RDWR) failed: No such file or directory (2)
Although there is a /tmp in the root .
Additionally i given this in the config.php upon searching the issue .
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = BASEPATH . 'tmp/';
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
Any ideas ??
Are you missing the "/" in the "BASEPATH".
Example "BASEPATH . '/tmp/';

Sharing session data to all subdomain codeigniter

I am trying to use one session data for all of my subdomains.
I have created a subdomain in cpanel like this : *.mydomain.in
and my *.mydomain.in uses the same path as my mydomain.in example:
mydomain.in uses path on my server: /public_html/mydomain.in
*.mydomain.in uses path on my server: /public_html/mydomain.in
Now the problem is every time I visit the site it's creating a different session. For example:
I visit mydomain.in .... it creates a session.
I visit example.mydomain.in .... it creates a different session
I again visit mydomain.in ... it creates a different session.
my codeigniter config file:
$config['encryption_key'] = 'MY-SECRET-KEY-HERE';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 0;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
$config['cookie_prefix'] = "";
$config['cookie_domain'] = ".mydomain.in";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;
Any help or suggestion would be a great help. Thanks in advance.
Here's how I solved the issue.
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 0;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update'] = 300000000;
$config['cookie_prefix'] = "etc_anything_";
$config['cookie_domain'] = ".mydomain.in";
$config['cookie_path'] = "/";
$config['cookie_secure'] = FALSE;
Mohamed Sufian's answer is largely correct, but note that as per the Session Docs, the cookie_prefix setting is ignored by the session driver. If you want to have an instance specific cookie name you will need to edit the sess_cookie_name configuration option.
For example:
$config['sess_cookie_name'] = 'my_prefix_ci_session';
$config['cookie_prefix'] = "my_prefix_"; // Only relevant for non-session cookies
$config['cookie_domain'] = ".example.com";
$config['cookie_path'] = "/";

codeigniter session expiration

I got a codeigniter session issue that I hasn't been able to solve. My sessions always expires even if my expiration time is set to "infinity". I've tried to set the expiration time to 0 and also a high number such as 604800 (one week).
At the moment my variables looks like this:
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 604800;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update'] = 300;
I've tried a lot of different combinations and a couple of different libraries but I canĀ“t seem to find a solution for the problem. Do I have to change gc_maxlifetime in my php.ini file to make it work?
Please change both the settings from TRUE to FALSE in your config.php files
$config['sess_use_database'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;

Resources