Search

CodeIgniter Flashdata

CodeIgniter supports “flashdata”, will only be available for the next request, and is then automatically clear “flashdata” session .

This method can be very useful, especially for one-time informational, error or status messages (for example: “User information has been success fully saved !”).

How Mnay Type Of Flashdata

Mainly there are two types predefined function in flash data

  1. mark_as_flash()
  2. set_flashdata()

mark_as_flash()

This function is used for this purpose, which takes only one argument of the value to be stored. We can also pass an array to store multiple values.

$this->session->mark_as_flash(array('item1', 'item2'));

set_flashdata()

This function can also be used, which takes two arguments, key and value, as shown below. We can also pass an array.

$this->session->set_flashdata('key','value');

Set Flashdata

//set_flashdata()
$this->session->set_flashdata('success','User information has been success fully saved');

And

//mark_as_flash()
$this->session->mark_as_flash(array('item1', 'item2'));

CodeIgniter Session Preferences

CodeIgniter will usually make everything work out of the box. However, Sessions data  are a very sensitive component of any application, so some careful configuration must be done. Please take your time to consider all of the options and their effects.

If you want to change session driver and storage path or any more session settings. you'll find the following Session related preferences in your application/config/config.php file:

Default session setting

$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;

Database Driver

The ‘database’ driver uses a relational database such as MySQL or PostgreSQL to store sessions

if you would like to use ‘tbl_sessions’ as your table name, you would do this:

$config['sess_driver'] 	  = 'database';
$config['sess_save_path'] = 'tbl_sessions';

Database Table For MySQL :

CREATE TABLE IF NOT EXISTS `tbl_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`)
);

Database Table For PostgreSQL:

CREATE TABLE "tbl_sessions" (
        "id" varchar(128) NOT NULL,
        "ip_address" varchar(45) NOT NULL,
        "timestamp" bigint DEFAULT 0 NOT NULL,
        "data" text DEFAULT '' NOT NULL
);

CREATE INDEX "ci_sessions_timestamp" ON "tbl_sessions" ("timestamp");

You will also need to add a PRIMARY KEY depending on your ‘sess_match_ip’ setting. The examples below work both on MySQL and PostgreSQL:

// When sess_match_ip = TRUE
ALTER TABLE ci_sessions ADD PRIMARY KEY (id, ip_address);

// When sess_match_ip = FALSE
ALTER TABLE ci_sessions ADD PRIMARY KEY (id);

// To drop a previously created primary key (use when changing the setting)
ALTER TABLE ci_sessions DROP PRIMARY KEY;

Stack Overlode is optimized Tutorials and examples for learning and training. Examples might be simplified to improve reading and learning and understand. Tutorials and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using Stack Overlode, you agree to have read and accepted our terms of use, cookie and privacy policy.