How to set cookie in codeigniter

Leave a Comment

Cookies in codeigniter



To use cookies in codeigniter we have to use its helper class.The Cookie Helper file contains functions that assist in working with cookies.
Below is the example to set cookies in get its values.So we create controller cookies_demo.php.Add the some method like the below example:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class cookies_demo extends CI_Controller {
 
 function __construct()
 {
  parent::__construct();
  $this->load->helper('cookie');
 }
 // to set cookies 
 function set()
 {
  $cookie= array(
      'name'   => 'demo',
      'value'  => 'Hello i m cookies which saved in this broswer',
       'expire' => '86500',
  );
  $this->input->set_cookie($cookie);
  echo "cookies successfully set";
 }
 // to get cookies
 function get()
 {
  echo $this->input->cookie('demo',true);
 }
}



Loading this Helper
This helper is loaded using the following code:
$this->load->helper('cookie');


The following functions are available:

$this->input->set_cookie()

Sets a cookie containing the values you specify. There are two ways to pass information to this function so that a cookie can be set: Array Method, and Discrete Parameters:

Array Method



Using this method, an associative array is passed to the first parameter:
$cookie = array(
    'name'   => 'demo',
      'value'  => 'Hello i m cookies which saved in this broswer',
       'expire' => '86500',
    'domain' => '.only4ututorials.blogspot.com',
    'path'   => '/',
    'prefix' => 'myprefix_',
    'secure' => TRUE
);

$this->input->set_cookie($cookie);


Notes:

  • Only the name and value are required. To delete a cookie set it with the expiration blank.
  • The expiration is set in seconds, which will be added to the current time. Do not include the time, but rather only the number of seconds from now that you wish the cookie to be valid. If the expiration is set to zero the cookie will only last as long as the browser is open.
  • For site-wide cookies regardless of how your site is requested, add your URL to the domain starting with a period, like this: .your-domain.com
  • The path is usually not needed since the function sets a root path.
  • The prefix is only needed if you need to avoid name collisions with other identically named cookies for your server.
  • The secure boolean is only needed if you want to make it a secure cookie by setting it to TRUE.
If after all this your cookies not set.Then check belows Points.

  • If you set 'secure' => TRUE.  which is only for https.
  • Always try to set 'expire' => 'something'.


Discrete Parameters
If you prefer, you can set the cookie by passing data using individual parameters:
$this->input->set_cookie($name, $value, $expire, $domain, $path, $prefix, $secure);



$this->input->cookie()
Lets you fetch a cookie. The first parameter will contain the name of the cookie you are looking for (including any prefixes):
$this->input->cookie('demo',true);

The function returns FALSE (boolean) if the item you are attempting to retrieve does not exist. The second optional parameter lets you run the data through the XSS filter. It's enabled by setting the second parameter to boolean TRUE;



Helpful links




Please comment down below if you have any query and please follows us for more awesome tutorials and keep motivating us .

0 comments:

Post a Comment

Powered by Blogger.