[ Index ] |
|
Code source de phpMyVisites 2.3 |
1 <?php 2 /* 3 * phpMyVisites : website statistics and audience measurements 4 * Copyright (C) 2002 - 2006 5 * http://www.phpmyvisites.net/ 6 * phpMyVisites is free software (license GNU/GPL) 7 * Authors : phpMyVisites team 8 */ 9 10 // $Id: Cookie.class.php 198 2007-01-17 16:47:59Z matthieu_ $ 11 12 13 /** 14 * Class that sets a cookie, read values, saves arrays in cookie, etc. 15 */ 16 class Cookie { 17 18 /** 19 * @var string 20 */ 21 var $name; 22 23 /** 24 * @var array cookie content 25 */ 26 var $a_content = array(); 27 28 var $expire; 29 30 /** 31 * Constructor 32 * 33 * @param string $nameCookie 34 */ 35 function Cookie($nameCookie='pmv_cookie_default') 36 { 37 $this->expire = COOKIE_EXPIRE; 38 39 $this->name=$nameCookie; 40 41 if($this->isDefined()) 42 { 43 $this->a_content = $this->get(); 44 } 45 46 //print($this->toString()); 47 48 } 49 50 function setExpire( $ts ) 51 { 52 $this->expire = $ts; 53 } 54 /** 55 * returns array contained in the cookie 56 * 57 * @return array 58 */ 59 function getContent() 60 { 61 return $this->a_content; 62 } 63 64 /** 65 * returns true if the cookie already exist, false else 66 * 67 * @return bool 68 */ 69 function isDefined() 70 { 71 return isset($_COOKIE[$this->name]); 72 } 73 74 /** 75 * returns the size of the cookie content, in bytes 76 */ 77 function getSize() 78 { 79 return strlen($_COOKIE[$this->name]); 80 } 81 82 function toString() 83 { 84 return "Cookie ". $this->name. " : \n<br>Current: ".varToString( $this->get())." \n<br>Next: ".varToString( $this->getContent() ); 85 } 86 87 /** 88 * returns the cookie's content array unserialized 89 * 90 * @return array 91 */ 92 function get() 93 { 94 $return = @unserialize(base64_decode( $_COOKIE[$this->name])); 95 if(is_array($return)) 96 { 97 return $return; 98 } 99 else 100 { 101 return array(); 102 } 103 } 104 105 /** 106 * returns the $varName value from the array in the cookie 107 * 108 * @param string $varName 109 * 110 * @return string|false 111 */ 112 function getVar($varName) 113 { 114 if(is_array($this->a_content) && isset($this->a_content[$varName])) 115 { 116 return secureVar($this->a_content[$varName]); 117 } 118 else 119 { 120 return false; 121 } 122 } 123 124 /** 125 * assigns a value to a variable in the cookie array 126 * 127 * @param string $varName variable name 128 * @param all $varValue 129 */ 130 function setVar($varName, $varValue) 131 { 132 if(is_array($varValue)) 133 { 134 $length = strlen(serialize($varValue)); 135 } 136 else 137 { 138 $length = strlen($varValue); 139 } 140 141 if($length < MAX_LENGTH_ONE_VALUE_IN_COOKIE) 142 { 143 $this->a_content[$varName] = $varValue; 144 return true; 145 } 146 else 147 { 148 printDebug("<br>Value '$varName' too big for the cookie, doesn't save..."); 149 return false; 150 } 151 } 152 153 /** 154 * saves the cookie on visitor computer, called once at the end of the whole process 155 * 156 * @return bool 157 */ 158 function save() 159 { 160 $this->p3p(); 161 $content = serialize($this->a_content); 162 $content = base64_encode ($content); 163 $this->setCookie($this->name, $content, time()+ $this->expire); 164 return true; 165 } 166 167 /** 168 * taken from http://usphp.com/manual/en/function.setcookie.php 169 * fix expires bug for IE users (should i say expected to fix the bug in 2.3 b2) 170 * TODO fix domain bug but we don't use it yet 171 */ 172 function setCookie($Name, $Value, $Expires, $Path = '', $Domain = '', $Secure = false, $HTTPOnly = false) 173 { 174 if (!empty($Domain)) 175 { 176 // Fix the domain to accept domains with and without 'www.'. 177 if (strtolower(substr($Domain, 0, 4)) == 'www.') $Domain = substr($Domain, 4); 178 179 $Domain = '.' . $Domain; 180 181 // Remove port information. 182 $Port = strpos($Domain, ':'); 183 if ($Port !== false) $Domain = substr($Domain, 0, $Port); 184 } 185 186 header('Set-Cookie: ' . rawurlencode($Name) . '=' . rawurlencode($Value) 187 . (empty($Expires) ? '' : '; expires=' . gmdate('D, d-M-Y H:i:s', $Expires) . ' GMT') 188 . (empty($Path) ? '' : '; path=' . $Path) 189 . (empty($Domain) ? '' : '; domain=' . $Domain) 190 . (!$Secure ? '' : '; secure') 191 . (!$HTTPOnly ? '' : '; HttpOnly'), false); 192 } 193 194 function delete() 195 { 196 $this->p3p(); 197 setcookie($this->name, false, time() - 3600); 198 return true; 199 } 200 201 function p3p() 202 { 203 header("P3P: CP='OTI DSP COR NID STP UNI OTPa OUR'"); 204 } 205 206 207 /** 208 * Init the phpmv cookie used in logging. Called when no previous phpmv cookie detected. 209 * 210 * @param string $uniqId Old uniqId if exists 211 * 212 * @return string uniqId assigned 213 */ 214 function put($uniqId='') 215 { 216 printDebug("<br>=>Cookie is init on visitor (idcookie and last_visit_time)<br>"); 217 if($uniqId=='') 218 { 219 $uniqId = md5(uniqid(rand())); 220 } 221 $this->setVar('idcookie', $uniqId); 222 $this->setVar('last_visit_time', todayTime()); 223 return $uniqId; 224 } 225 } 226 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 14:10:01 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |