Cookie management in CodeIgniter
0 1604
- A cookie is most commonly used to identify a user.
- It is a small file stored by the server on the client’s computer.
- Codeigniter provides a cookie helper to handle the cookies.
- The Cookie Helper file contains functions that assist in working with cookies.
Related Topics:
Codeigniter Interview Questions
Session management in Codeigniter
Tempdata in Codeigniter
Loading this Helper:
The following code is used to load the cookie helper.
$this->load->helper('cookie');
Available Functions:
1 set_cookie(): This helper function is used to set a browser cookie.
Syntax:
set_cookie($name[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]]]);
Parameter Description:
- $name (mixed) − Cookie name or associative array of all of the parameters available to this function
- $value (string) − Cookie value
- $expire (int) − Number of seconds until expiration
- $domain (string) − Cookie domain (usually: .yourdomain.com)
- $path (string) − Cookie path
- $prefix (string) − Cookie name prefix
- $secure (bool) − Whether to only send the cookie through HTTPS
- $httponly (bool) − Whether to hide the cookie from JavaScript
The return type of this function is void.
2 get_cookie(): This function is used to get the browser cookie.
Syntax:
get_cookie($index[, $xss_clean = NULL]]);
Parameter Description:
- $index (string) − Cookie name
- $xss_clean (bool) − Whether to apply XSS filtering to the returned value
The return type of this function is mixed.
3 delete_cookie(): This function is used to delete a cookie.
Syntax:
delete_cookie($name[, $domain = ''[, $path = '/'[, $prefix = '']]]]);
Parameter Description:
- $name (string) − Cookie name
- $domain (string) − Cookie domain (usually: .yourdomain.com)
- $path (string) − Cookie path
- $prefix (string) − Cookie name prefix
The return type of this function is void.
Example:
Step 1 Open the application/views directory and create a new view file cookie_view.php.
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<h2>cookie example</h2>
<a href = 'Cookie_controller/display_cookie'>Click Here</a> to view the cookie.<br>
<a href = 'Cookie_controller/deletecookie'>Click Here</a> to delete the cookie.
</div>
</body>
</html>
Step 2 Open the application/controller directory and create a new controller Cookie_controller.php.
<?php
class Cookie_controller extends CI_Controller {
function __construct() {
parent::__construct();
$this->load->helper(array('cookie', 'url')); //load cookie helper
}
public function index() {
set_cookie('example','cookie example on CodingTag','3600'); // set cookie example
$this->load->view('cookie_view');
}
public function display_cookie() {
echo get_cookie('example');
$this->load->view('cookie_view');
}
public function deletecookie() {
delete_cookie('example');
redirect('Cookie_controller/display_cookie');
}
}
?>
Step 3 Open the given URL into the browser.
http://localhost/ci/index.php/Cookie_controller
After clicking on the first link, the cookie will be set.
After clicking on the second link, it will be deleted.
Share:
Comments
Waiting for your comments