Codeigniter - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

CodeIgniter Cookie

CodeIgniter Cookie

shape Introduction

Web applications work on HTTP protocol, a stateless protocol i.e once the web server processes a request for a web page, the web server will not remember anything about the client who has made the request. To overcome the problem, CodeIgniter Cookie came into existence.

shape Description

CodeIgniter Cookie is the small text file that is stored in the browser of visitor’s computer. Cookies help the server to quickly access the information of users who visit the same pages again and again. CodeIgniter uses Cookie Helper function to organize Cookies.
set_cookie() function sets the cookie in which all the values can be passed in two ways. In the first process, only the array will be passed and in the second process, each parameter is passed individually. The syntax for set_cookie() function is as follows. [php]set_cookie($name[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]])[/php] where, $name(mixed) => Name of Cookie or the respective array with parameters available to the function. $value (string) => Value of Cookie $expire (int) => Expiration duration of Cookie in seconds $domain (string) => Domain of Cookie (Mostly: .yourdomain.com) $path (string) => Path of Cookie $prefix (string) => Prefix name of Cookie $secure (bool) => Decision factor of to confirm in forwarding the cookie through HTTPS $httponly (bool) => Deciding factor in hiding the cookie from browser JavaScript
Using get_cookie() function, the value of specified cookie set in setcookie() function can be returned and the syntax will be as follows. [php] get_cookie($index[, $xss_clean = NULL]]) [/php] where, $index (string) => name of cookie $xss_clean (bool) => checks condition to apply XSS filtering to the returned value or not
delete_cookie() function deletes the cookie. [php] delete_cookie($name[, $domain = ''[, $path = '/'[, $prefix = '']]]]) [/php] where, $name(mixed) => Name of Cookie $domain (string) => Domain of Cookie (Mostly: .yourdomain.com) $path (string) => Path of Cookie $prefix (string) => Prefix name of Cookie

shape Example

Step-1 : Before creating Controller and view of CodeIgniter Cookie, configure Cookie Helper in the file autoload.php present in the file path application/config using the below code. [php]$autoload['helper'] = array('url','cookie');[/php] Step-2 : Create the controller Cookie_controller.php in the file path application/controller and enter the below code. [php] <?php class Cookie_controller extends CI_Controller { function __construct() { parent::__construct(); $this->load->helper(array('cookie', 'url')); } public function index() { set_cookie('cookie_name','cookie_value','3600'); $this->load->view('view_cookie'); } public function display_cookie() { echo get_cookie('cookie_name'); $this->load->view('view_cookie'); } public function deletecookie() { delete_cookie('cookie_name'); redirect('cookie/display'); } } ?> [/php] Step-3: Create a view view_cookie.php in the file path application/views and enter the below code. [php] <!DOCTYPE html> <html lang = "en"> <head> <meta charset = "utf-8"> <title>CodeIgniter View Example</title> </head> <body> <a href = 'display'>Click Here</a> to view the cookie.<br> <a href = 'delete'>Click Here</a> to delete the cookie. </body> </html> [/php] Step-4: Add the route to the above created controller in routes.php file present in application/config and enter the below code at the end of the file. [php] $route['cookie'] = "Cookie_controller"; $route['cookie/display'] = "Cookie_controller/display_cookie"; $route['cookie/delete'] = "Cookie_controller/deletecookie"; [/php] Now check the output by entering the URL, [html]http://localhost/Codeigniter/index.php/cookie[/html] Output:

Summary

shape Key Points

  • CodeIgniter Cookie is the small text file that is stored in the browser of visitor’s computer.
  • set_cookie(), get_cookie() and delete_cookie() are the three functions involved in CodeIgniter Cookie.