CodeIgniter Session
Open the file config.php from the directory root/application/config/ location. Edit the page as following
$config['encryption_key'] = 'Your Encryption Key';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = TRUE;
$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'] = 300;
Where
'sess_cookie_name' = the name you want for the cookie
'sess_expiration' = the number of SECONDS you want the session to last.
by default sessions last 7200 seconds (two hours). Set to zero for no expiration.
'sess_expire_on_close' = Whether to cause the session to expire automatically
when the browser window is closed
'sess_encrypt_cookie' = Whether to encrypt the cookie
'sess_use_database' = Whether to save the session data to a database
'sess_table_name' = The name of the session database table
'sess_match_ip' = Whether to match the user's IP address when reading the session data
'sess_match_useragent' = Whether to match the User Agent when reading the session data
'sess_time_to_update' = how many seconds between CI refreshing Session Information
As we have set 'session_use_database' value is true and 'sess_table_name' value is 'ci_sessions'. So create a table your database file name as ci_sessions. The table structure is as follows:
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`)
);
By default the table is called ci_sessions, but you can name it anything you want as long as you update the application/config/config.php file so that it contains the name you have chosen
Now open the autoload.php file from root/application/config/ location and edit the file as following
$autoload['libraries'] = array('session');
Now I have created a controller in the root/application/controllers/ directory. The file name is sessionauto.php file and edit the file as follow
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Sessionauto extends CI_Controller
{
public function index()
{
$this -> load -> view('session_auto');
}
}
Now create a file in the view in the root/application/views directory . I have named it seesion_auto.php. Edit the file as follows
<!DOCTYPE html>
<html>
<head>
<title>Auto Set Session View</title>
</head>
<body>
<b>Session ID :</b> <?php echo $this -> session -> userdata('session_id'); ?> <br />
<b>IP Address :</b> <?php echo $this -> session -> userdata('ip_address'); ?> <br />
<b>User Agent :</b> <?php echo $this -> session -> userdata('user_agent'); ?> <br />
<b>Last Activity :</b> <?php echo $this -> session -> userdata('last_activity'); ?> <br />
</body>
</html>
Now if you run the controller then you will get the result something like this
Session ID : 1314d5a77244c256605bbf2bcf9e93f5
IP Address : 127.0.0.1
User Agent : Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.47 Safari/536.11
Last Activity : 1341385732
If you look into your created table field value then you will get the same value as view. But the session_id value will change every 5mins as we have set the sess_time_to_update =300. Here 300 is seconds.
$config['sess_time_to_update'] = 300;
Comments
Post a Comment