blob: b117816e265c992a205d1485947d04985782638a [file] [log] [blame]
adminb0dd10f2006-08-25 17:25:49 +00001<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
2/**
3 * Code Igniter
4 *
5 * An open source application development framework for PHP 4.3.2 or newer
6 *
7 * @package CodeIgniter
8 * @author Rick Ellis
9 * @copyright Copyright (c) 2006, pMachine, Inc.
admine334c472006-10-21 19:44:22 +000010 * @license http://www.codeignitor.com/user_guide/license.html
adminb0dd10f2006-08-25 17:25:49 +000011 * @link http://www.codeigniter.com
12 * @since Version 1.0
13 * @filesource
14 */
admine334c472006-10-21 19:44:22 +000015
adminb0dd10f2006-08-25 17:25:49 +000016// ------------------------------------------------------------------------
17
18/**
19 * Code Igniter Cookie Helpers
admine334c472006-10-21 19:44:22 +000020 *
adminb0dd10f2006-08-25 17:25:49 +000021 * @package CodeIgniter
22 * @subpackage Helpers
23 * @category Helpers
24 * @author Rick Ellis
25 * @link http://www.codeigniter.com/user_guide/helpers/cookie_helper.html
26 */
27
28// ------------------------------------------------------------------------
29
30/**
31 * Set cookie
32 *
admine334c472006-10-21 19:44:22 +000033 * Accepts six parameter, or you can submit an associative
adminb0dd10f2006-08-25 17:25:49 +000034 * array in the first parameter containing all the values.
35 *
36 * @access public
37 * @param mixed
38 * @param string the value of the cookie
39 * @param string the number of seconds until expiration
40 * @param string the cookie domain. Usually: .yourdomain.com
41 * @param string the cookie path
42 * @param string the cookie prefix
43 * @return void
44 */
45function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '')
admine334c472006-10-21 19:44:22 +000046{
adminb0dd10f2006-08-25 17:25:49 +000047 if (is_array($name))
48 {
49 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'name') as $item)
50 {
51 if (isset($name[$item]))
52 {
53 $$item = $name[$item];
54 }
55 }
56 }
57
admin885b0342006-09-21 05:36:15 +000058 // Set the config file options
admin75198f92006-10-07 03:16:53 +000059 $CI =& get_instance();
admin885b0342006-09-21 05:36:15 +000060
admin75198f92006-10-07 03:16:53 +000061 if ($prefix == '' AND $CI->config->item('cookie_prefix') != '')
admin885b0342006-09-21 05:36:15 +000062 {
admin75198f92006-10-07 03:16:53 +000063 $CI->config->item('cookie_prefix');
admin885b0342006-09-21 05:36:15 +000064 }
admin75198f92006-10-07 03:16:53 +000065 if ($domain == '' AND $CI->config->item('cookie_domain') != '')
admin885b0342006-09-21 05:36:15 +000066 {
admin75198f92006-10-07 03:16:53 +000067 $CI->config->item('cookie_domain');
admin885b0342006-09-21 05:36:15 +000068 }
admin75198f92006-10-07 03:16:53 +000069 if ($prefix == '/' AND $CI->config->item('cookie_path') != '/')
admin885b0342006-09-21 05:36:15 +000070 {
admin75198f92006-10-07 03:16:53 +000071 $CI->config->item('cookie_path');
admin885b0342006-09-21 05:36:15 +000072 }
73
adminb0dd10f2006-08-25 17:25:49 +000074 if ( ! is_numeric($expire))
75 {
76 $expire = time() - 86500;
77 }
78 else
79 {
80 if ($expire > 0)
81 {
82 $expire = time() + $expire;
83 }
84 else
85 {
86 $expire = 0;
87 }
88 }
89
90 setcookie($prefix.$name, $value, $expire, $path, $domain, 0);
91}
admin885b0342006-09-21 05:36:15 +000092
93// --------------------------------------------------------------------
94
95/**
96 * Fetch an item from the COOKIE array
97 *
98 * @access public
99 * @param string
100 * @param bool
101 * @return mixed
102 */
103function get_cookie($index = '', $xss_clean = FALSE)
104{
admin75198f92006-10-07 03:16:53 +0000105 $CI =& get_instance();
106 return $CI->input->cookie($index, $xss_clean);
admin885b0342006-09-21 05:36:15 +0000107}
108
109// --------------------------------------------------------------------
110
111/**
112 * Delete a COOKIE
113 *
114 * @param mixed
115 * @param string the cookie domain. Usually: .yourdomain.com
116 * @param string the cookie path
117 * @param string the cookie prefix
118 * @return void
119 */
120function delete_cookie($name = '', $domain = '', $path = '/', $prefix = '')
121{
122 set_cookie($name, '', '', $domain, $path, $prefix);
123}
124
adminb0dd10f2006-08-25 17:25:49 +0000125
126?>