note 76334 added to function.setcookie

From: Date: Wed, 11 Jul 2007 05:12:41 +0000
Subject: note 76334 added to function.setcookie
Groups: php.notes 
Request: Send a blank email to php-notes+get-128372@lists.php.net to get a copy of this message
<?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); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 190.16.130.26 ---- Manual Page -- http://www.php.net/manual/en/function.setcookie.php Edit -- https://master.php.net/note/edit/76334 Del: integrated -- https://master.php.net/note/delete/76334/integrated Del: useless -- https://master.php.net/note/delete/76334/useless Del: bad code -- https://master.php.net/note/delete/76334/bad+code Del: spam -- https://master.php.net/note/delete/76334/spam Del: non-english -- https://master.php.net/note/delete/76334/non-english Del: in docs -- https://master.php.net/note/delete/76334/in+docs Del: other reasons-- https://master.php.net/note/delete/76334 Reject -- https://master.php.net/note/reject/76334 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128372) next »