Reputation: 3
I am using codeigniter 3.1, I want to track users with their username and userid from the sessions and store them into database table. so i have created codeigniter 3.1 session table in database. Here it is
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(40) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`user_id` varchar(45) NOT NULL,
`username` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
PRIMARY KEY (id),
KEY `ci_sessions_timestamp` (`timestamp`)
);
And also changed config.php
sessions also here it is
$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_sessions';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';//its your table name name
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
Here is my controller function for session
public function session_test($value='')
{
$newdata = array(
'username' => 'johndoe',
'user_id' => 3,
'logged_in' => TRUE
);
$this->session->set_userdata("userdata",$newdata);
$session_id = $this->session->userdata('userdata');
echo "<pre>";
print_r($session_id);
echo "</pre>";
}
Here output is showing in the browser is
Array
(
[username] => johndoeasdas
[user_id] => [email protected]
[logged_in] => 1
)
and storing in the database Here is image which are session storing in database are
Here i am unable to store username and user_id from sessions into database.
How to store username and user_id from session into database table.
Thanks for all suggestions.
Upvotes: 0
Views: 1673
Reputation: 8964
You do not need to change the structure of ci_session
in fact you SHOULD NOT change the table structure. Use the table structure from CodeIgniter's documentation.
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`id` varchar(128) NOT NULL,
`ip_address` varchar(45) NOT NULL,
`timestamp` int(10) unsigned DEFAULT 0 NOT NULL,
`data` blob NOT NULL,
KEY `ci_sessions_timestamp` (`timestamp`)
);
Anything you add to session data will be stored in the "data" column of the table.
Session data is already an array. You only make life harder by putting an array of data as an session data item. This test controller results in much easier access to any given session data item.
public function session_test($value = '')
{
$newdata = array(
'username' => 'johndoe',
'user_id' => 3,
'logged_in' => TRUE
);
$this->session->set_userdata($newdata);
echo "User Name: ".$this->session->username."<br>";
echo "User ID: ".$this->session->user_id."<br>";
echo "Is logged in: ";
echo $this->session->logged_in ? 'Yes' : 'No';
}
Upvotes: 0
Reputation: 71
CREATE TABLE IF NOT EXISTS `ci_sessions` (
session_id varchar(40) DEFAULT '0' NOT NULL,
ip_address varchar(45) DEFAULT '0' NOT NULL,
user_agent varchar(120) NOT NULL,
last_activity int(10) unsigned DEFAULT 0 NOT NULL,
user_data text NOT NULL,
PRIMARY KEY (session_id),
KEY `last_activity_idx` (`last_activity`)
);
This table will store your session data. Now we need to set config variable. Go to “application/config/config.php” and update below variable:
$config['sess_driver'] = 'database'; //by deafault it may be 'files'
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Now store the session data using
$data = array(
'name' => 'xyz', //some value
'is_logged_in' => TRUE
);
$this->session->set_userdata($data);
for more details please visit https://www.codeigniter.com/userguide3/libraries/sessions.html
Upvotes: 2