Ticket #6: cookie_helper.patch
| File cookie_helper.patch, 4.2 kB (added by phoop, 20 months ago) |
|---|
-
cookie_helper.php
36 36 * @access public 37 37 * @param mixed 38 38 * @param string the value of the cookie 39 * @param stringthe number of seconds until expiration39 * @param int the number of seconds until expiration 40 40 * @param string the cookie domain. Usually: .yourdomain.com 41 41 * @param string the cookie path 42 42 * @param string the cookie prefix 43 * @param bool whether the cookie should be sent ONLY over SSL 43 44 * @return void 44 45 */ 45 function set_cookie($name = '', $value = '', $expire = '', $domain = '', $path = '/', $prefix = '')46 function set_cookie($name, $value = '', $expire = 0, $domain = null, $path = null, $prefix = null, $secure = false) 46 47 { 47 48 if (is_array($name)) 48 { 49 foreach (array('value', 'expire', 'domain', 'path', 'prefix', ' name') as $item)49 { 50 foreach (array('value', 'expire', 'domain', 'path', 'prefix', 'secure', 'name') as $item) 50 51 { 51 52 if (isset($name[$item])) 52 53 { … … 54 55 } 55 56 } 56 57 } 57 58 58 59 // Set the config file options 59 60 $CI =& get_instance(); 60 61 if ($prefix == '' AND $CI->config->item('cookie_prefix') != '') 61 if ( ! is_numeric($expire)) 62 62 { 63 $CI->config->item('cookie_prefix'); 63 $default_cookie_lifetime = $CI->config->item('cookie_lifetime'); 64 $expire = 65 (! is_numeric($default_cookie_lifetime) || $default_cookie_lifetime<=0) ? 66 0 : 67 (time() + $default_cookie_lifetime); 64 68 } 65 if ($domain == '' AND $CI->config->item('cookie_domain') != '')69 else 66 70 { 67 $CI->config->item('cookie_domain'); 71 $expire = 72 ($expire==0) ? 73 $expire : 74 (time() + $expire); 68 75 } 69 if ($prefix == '/' AND $CI->config->item('cookie_path') != '/') 76 $default_domain = $CI->config->item('cookie_domain'); 77 if (is_null($domain) AND !empty($default_domain)) 70 78 { 71 $ CI->config->item('cookie_path');79 $domain = $default_domain; 72 80 } 73 74 if ( ! is_numeric($expire))81 $default_path = $CI->config->item('cookie_path'); 82 if (is_null($path) AND ! empty($default_path)) 75 83 { 76 $ expire = time() - 86500;84 $path = $default_path; 77 85 } 78 else 86 $default_prefix = $CI->config->item('cookie_prefix'); 87 if (is_null($prefix) AND ! empty($default_prefix)) 79 88 { 80 if ($expire > 0) 81 { 82 $expire = time() + $expire; 83 } 84 else 85 { 86 $expire = 0; 87 } 89 $prefix = $default_prefix; 88 90 } 89 90 setcookie($prefix.$name, $value, $expire, $path, $domain, 0); 91 $secure = is_bool($secure) ? $secure : false; 92 93 setcookie($prefix.$name, $value, $expire, $path, $domain, $secure); 91 94 } 92 95 93 96 // -------------------------------------------------------------------- 94 97 95 98 /** … … 97 100 * 98 101 * @access public 99 102 * @param string 103 * @param string 100 104 * @param bool 101 105 * @return mixed 102 106 */ 103 function get_cookie($ index = '', $xss_clean = FALSE)107 function get_cookie($name = '', $prefix=null, $xss_clean = FALSE) 104 108 { 105 109 $CI =& get_instance(); 106 return $CI->input->cookie($index, $xss_clean); 110 $default_prefix = $CI->config->item('cookie_prefix'); 111 if (is_null($prefix) AND ! empty($default_prefix)) 112 { 113 $prefix = $default_prefix; 114 } 115 return $CI->input->cookie($prefix.$name, $xss_clean); 107 116 } 108 117 109 118 // -------------------------------------------------------------------- … … 111 120 /** 112 121 * Delete a COOKIE 113 122 * 123 * @access public 114 124 * @param mixed 115 125 * @param string the cookie domain. Usually: .yourdomain.com 116 126 * @param string the cookie path 117 127 * @param string the cookie prefix 128 * @param bool 118 129 * @return void 119 130 */ 120 function delete_cookie($name = '', $domain = '', $path = '/', $prefix = '')131 function delete_cookie($name = '', $domain = null, $path = null, $prefix = null, $remove_live = TRUE) 121 132 { 122 set_cookie($name, '', '', $domain, $path, $prefix); 133 if($remove_live===TRUE) 134 { 135 $CI =& get_instance(); 136 $default_prefix = $CI->config->item('cookie_prefix'); 137 if (is_null($prefix) AND ! empty($default_prefix)) 138 { 139 $prefix = $default_prefix; 140 } 141 $cIndex = is_array($name) ? $name['name'] : $name; 142 if(isset($_COOKIE[$prefix.$cIndex])) 143 { 144 unset($_COOKIE[$prefix.$cIndex]); 145 } 146 } 147 set_cookie($name, '', -86500, $domain, $path, $prefix); 123 148 } 124 149 125 150
