Cookie Helper


Codeigniter Cookie Helper contains the functions that are used in working with cookies. Here in this tutorial we are going to explain how you can create cookies using this helper. Let us go step by step to learn about the Cookie Helper.


Cookie Helper in Codeigniter Example

Load This Helper

Before using the helper function you must load this helper as below โ€“

Cookie Helper in Codeigniter Example:

$this->load->helper('cookie');

After Loading this function you can use itโ€™s functions.

Cookie Helper Functions

Let us go through the available functions in this helper.

set_cookie Function

Syntax of set_cookie function

Codeigniter set_cookie function syntax:

set_cookie($name, $value = '', $expire = '', $domain = '', $path = '/', $prefix = '', $secure = FALSE, $httponly = FALSE);

Parameters:

  • $name (mixed) โ€“ Name of Cookie.
  • $value (string) โ€“ Cookie value.
  • $expire (int) โ€“ Number of seconds until expiration.
  • $domain (string) โ€“ Cookie domain ex.- www.tutorialsplane.com.
  • $path (string) โ€“ Cookie path.
  • $prefix (string) โ€“ Cookie name prefix.
  • $secure (bool) โ€“ To send the cookie using HTTPS.
  • $httponly (bool) โ€“ To hide the cookie from JavaScript.

get_cookie Function

Syntax of get_cookie function

Codeigniter get_cookie function syntax:

get_cookie($index, $xss_clean = NULL);

Parameters:

  • $index (string) โ€“ Name of Cookie.
  • $xss_clean (bool) โ€“ If you want to apply xss filtering to returned value set true else it will be false by default.

Returns:

Returns cookie value. If value not found it will return NULL.

delete_cookie Function

Syntax of delete_cookie function

Codeigniter delete_cookie function syntax:

delete_cookie($index, $domain, $path, $prefix);

Parameters:

  • $name (mixed) โ€“ Name of Cookie.
  • $domain (string) โ€“ Cookie domain ex.- www.tutorialsplane.com.
  • $path (string) โ€“ Cookie path.
  • $prefix (string) โ€“ Cookie name prefix.
  • Returns:

    Returns void.

    delete_cookie Example

    If you do not want to specify other params you can delete the cookies as below โ€“

    Codeigniter delete_cookie Example:

    delete_cookie('my_cookie');
    //my_cookie name of the cookies
    

    Where my_cookie is name of the cookie.


    Examples

    Letโ€™s have look over examples on set, get and delete cookie.


    Codeigniter Set Cookie Example

    You can set cookie value simply as below โ€“

    Codeigniter Set Cookie Example :

    public function setMyCookie(){
    	
    $this->load->helper('cookie');	
    $cookie = array(
            'name'   => 'username',
            'value'  => 'test@tutorialsplane.com',
            'expire' => time()+86500,
            'domain' => 'www.tutorialsplane.com', // You can add .localhost for local machine
            'path'   => '/',
            'prefix' => 'tutorialsplane_',
            );
     
    set_cookie($cookie);
    
    }
    

    Codeigniter Get Cookie Example

    You can get cookie value simply as below โ€“

    Codeigniter Get Cookie Example :

    public function getMyCookie(){
    	
    $this->load->helper('cookie');
    //get_cookie('prefix_name');
    $cookieVal = get_cookie('tutorialsplane_username');
    echo $cookieVal;
    
    }
    

    Codeigniter Delete Cookie Example

    You can delete cookie value simply as below โ€“

    Codeigniter delete Cookie Example :

    public function deleteMyCookie(){
    	
    $this->load->helper('cookie');
    // delete cookie example
    $cookie = array(
        'name'   => 'username',
        'value'  => '',
        'expire' => '0',
        'domain' => 'www.tutorialsplane.com', // You can add .localhost for local machine
        'prefix' => 'tutorialsplane_'
        );
     
    delete_cookie($cookie);
    
    }
    

    The above example will delete the cookie set in the above example.

    Tip : Make Sure you load the cookie helper before using the cookie functions.

    Advertisements

    Add Comment

    ๐Ÿ“– Read More