note 76334 deleted from function.setcookie by nlopess
| From: | nlopess@php.net | Date: | Fri, 13 Jul 2007 19:25:15 +0000 |
| Subject: | note 76334 deleted from function.setcookie by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-128495@lists.php.net to get a copy of this message | ||
Note Submitter: martin dot clasen at gmail dot com
----
<?php
/**
* Project: ROX Cookie
* File: RoxCookie.php
* Author: MartÃn Clasen <martin.clasen AT gmail DOT com>
* Website: http://www.clasen.com.ar
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* Author: MartÃn Clasen <martin.clasen AT gmail DOT com>
* @version 1.5
*/
class RoxCookie
{
function set($name, $value=false, $expires=1, $path='/', $domain='',
$secure=false)
{
if(is_array($name)) {
foreach($name as $k=>$v)
RoxCookie::set($k, $v, $expires, $path, $domain, $secure);
} elseif(is_array($value)) {
foreach($value as $k=>$v)
RoxCookie::set($name.'['.$k.']', $v, $expires, $path, $domain,
$secure);
} elseif($value!==false) {
$expires = $expires*60*60*24;
setcookie($name, $value, time()+$expires, $path, $domain, $secure);
eval('$_COOKIE'.RoxCookie::_brFix($name).' = strval($value);');
}
}
function get($name)
{
eval('$r = $_COOKIE'.RoxCookie::_brFix($name).";");
return $r;
}
function del($name,$path='/',$domain='')
{
$names = explode(',',$name);
foreach($names as $name)
{
eval('$k = &$_COOKIE'.RoxCookie::_brFix($name).";");
if(is_array($k))
{
$k[''] = false;
foreach($k as $key=>$value)
RoxCookie::del($name.'['.$key.']');
}
setcookie($name, '', time()-1000, $path, $domain);
eval('unset($_COOKIE'.RoxCookie::_brFix($name).");");
}
}
function _brFix($name)
{
return "['".strtr($name, array("'"=>"\'",
']'=>'', '['=>"']['"))."']";
}
function sync()
{
$sCookie = $_COOKIE;
RoxCookie::clean();
RoxCookie::set($sCookie);
}
function clean()
{
if (isset($_SERVER['HTTP_COOKIE']))
{
$cookies = explode(';', $_SERVER['HTTP_COOKIE']);
foreach($cookies as $cookie)
{
$parts = explode('=', $cookie);
$name = trim($parts[0]);
RoxCookie::del($name);
}
}
$_COOKIE = array();
}
}
//TESTING CASES
RoxCookie::clean();
// cookie value : Array()
RoxCookie::set('user','tincho');
// cookie value : Array ( [user] => tincho )
RoxCookie::set('status',array(1,2,3));
// cookie value : Array ( [user] => tincho, [status] => Array ( [0] => 1 [1] => 2 [2]
=> 3 ) )
RoxCookie::del('user');
// cookie value : Array ( [status] => Array ( [0] => 1 [1] => 2 [2] => 3 ) )
RoxCookie::clean();
// cookie value : Array()
RoxCookie::set(array(1,2,3));
// cookie value : Array ( [0] => 1 [1] => 2 [2] => 3 )
RoxCookie::set(array('A'=>1,'B'=>2,'C'=>3));
// cookie value : Array ( [0] => 1, [1] => 2, [2] => 3, [A] => 1, [B] => 2, [C] =>
3 )
RoxCookie::del('A');
// cookie value : Array ( [0] => 1, [1] => 2, [2] => 3, [B] => 2, [C] => 3 )
RoxCookie::del('0,C');
// cookie value : Array ( [1] => 2, [2] => 3, [B] => 2 )
RoxCookie::clean();
// cookie value : Array()
$_COOKIE['user'] = 'tincho';
RoxCookie::sync();
// cookie value : Array ( [user] => tincho )
print_r($_COOKIE);
?>