FuelPHP - Cookie & Session Management


Advertisements

Cookie provides client side data storage and it supports only a small amount of data. Usually, it is 2KB per domain and it depends on the browser. Session provides server side data storage and it supports a large amount of data. Let us go through how to create cookie and session in FuelPHP web application.

Cookies

FuelPHP provides a Cookie class to create a cookie item. Cookie class is used to create, assign, and delete cookies.

Configure Cookie

The Cookie class can be configured globally through the main application configuration file, located at fuel/app/config/config.php. It is defined as follows.

'cookie' => array (  
   
   //Number of seconds before the cookie expires 
   'expiration'  => 0,  
   
   //Restrict the path that the cookie is available to 
   'path'        => '/',  
   
   //Restrict the domain that the cookie is available to 
   'domain'      => null,  
   
   // Only transmit cookies over secure connections 
   'secure'      => false,  
   
   // Only transmit cookies over HTTP, disabling Javascript access 
   'http_only'   => false, 
), 

Methods

Cookie class provides methods to create, access, and delete a cookie item. They are as follows −

set()

set method is used to create a Cookie variable. It contains the following arguments,

  • $name − The key in the $_COOKIE array.

  • $value − The value of the cookie.

  • $expiration − Number of seconds the cookie should last for.

  • $path − The path on the server in which the cookie will be available on.

  • $domain − The domain that the cookie is available to.

  • $secure − Set to true if you only want to transmit cookies over secure connections.

  • $httponly − Allow only transmit of cookies over HTTP, disabling JavaScript access.

Cookie::set('theme', 'green');

get()

The get method is used to read a Cookie variable. It contains the following arguments,

  • $name − The key in the $_COOKIE array.

  • $value − The value to return if the key is not available i the $_COOKIE array.

Cookie::get('theme');

delete()

The delete method is used to delete a Cookie variable. It contains the following arguments,

  • $name − The key in the $_COOKIE array.

  • $value − The value of the cookie.

  • $domain − The domain that the cookie is available to.

  • $secure − Set to true if you only want to transmit cookies over secure connections.

  • $httponly − Allow only to transmit cookies over HTTP, disabling JavaScript access.

Cookie::delete('theme');

Session

FuelPHP provides class, Session to maintain state of the application.

Configure Session

Session class can be configured through the special configuration file, fuel/core/config/session.php. Some of the important configuration entries are as follows −

  • auto_initialize − Initialize the session automatically.

  • driver − Name of the session driver. Session is implemented using driver and the possible options are cookie, db, memcached, redis, and file. The default driver is cookie.

  • match_ip − Check the client IP.

  • match_ua − Check the client user agent.

  • expiration_time − Session time out value in seconds.

  • rotation_time − Time to renew the session.

Session Methods

Session class provides methods to manipulate the session data. They are as follows,

instance()

The instance method returns a default or a specific instance, which is identified by name.

$session = Session::instance();            // default instance 
$session = Session::instance('myseesion'); // specific instance

set()

The set method is used to assign a Session variable.

Session::set('userid', $userid);

get()

The get method allows you to retrieve the stored variables from the session.

$userid = Session::get('userid'); 

delete()

The delete method allows you to delete a stored session variable.

Session::delete('userid');

create()

The create method allows you to create a new session. If a session is already present, it will be destroyed and a new session is created.

Session::create(); 

destroy()

The destroy method is used to destroy an existing session.

Session::destroy();

read()

The read method allows you to read a session.

Session::read(); 

write()

The write method allows you to write the session.

Session::write();

key()

The key method allows you retrieve elements of the session key. The value of the key is unique.

$session_id = Session::key('session_id'); 
Advertisements