CodeIgniter - Cookie Management
Syntax | set_cookie($name[, $value = ''[, $expire = ''[, $domain = ''[, $path = '/'[, $prefix = ''[, $secure = FALSE[, $httponly = FALSE]]]]]]]]) |
Parameters |
|
Return Type | void |
Syntax | get_cookie($index[, $xss_clean = NULL]]) |
Parameters |
|
Return | The cookie value or NULL if not found |
Return Type | mixed |
Syntax | delete_cookie($name[, $domain = ''[, $path = '/'[, $prefix = '']]]]) |
Parameters |
|
Return Type | void |
Example
<?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('Cookie_view');
}
public function display_cookie() {
echo get_cookie('cookie_name');
$this->load->view('Cookie_view');
}
public function deletecookie() {
delete_cookie('cookie_name');
redirect('cookie/display');
}
}
?>
<!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>
$route['cookie'] = "Cookie_controller";
$route['cookie/display'] = "Cookie_controller/display_cookie";
$route['cookie/delete'] = "Cookie_controller/deletecookie";
http://yoursite.com/index.php/cookie
