Código PHP:
/*********************
* Clase Cookie v0.2.1 *
* www.developtus.com *
**********************/
class Cookie{
public static function set($cookieName,$value='',$time=3600,$path=null,$domain=null,$httpOnly=false){
setcookie($cookieName,$value,time()+(int)$time,$path,$domain,(bool)$httpOnly);
$_COOKIE[$cookieName]=$value;
}
public static function get($cookieName,$defaultValue=''){
return self::exists($_COOKIE[$cookieName])?$_COOKIE[$cookieName]:$defaultValue?$defaultValue:false;
}
public static function del($cookieName,$value='',$time=-3600,$path=null,$domain=null,$httpOnly=false){
self::set($cookieName,$value,$time,$path,$domain,$httpOnly);
unset($_COOKIE[$cookieName]);
}
public static function exists($cookieName){
return isset($_COOKIE[$cookieName]);
}
public static function isEmpty($cookieName){
return empty($_COOKIE[$cookieName]);
}
public static function delAll(){
$count=count($_COOKIE);
$names=array_keys($_COOKIE);
for($i=0;$i<$count;$i++){
self::del($names[$i]);
}//for()
}
}
Código PHP:
include_onde('class.cookie.php');
Cookie::set('var1','hola mundo!',60); //crear cookie de nombre var1 con valor "hola mundo" y que dure un minuto
echo Cookie::get('var1'); //imprime el valor de la cookie var1
Cookie::set('copiaVar1',Cookie::get('var1'));//copiar var1 a una nueva cookie llamada copiavar1
if(Cookie::exists('copiaVar1')) {echo "existe la cookie";}//comprueba si existe la cookie
if(!Cookie::isEmpty('copiaVar1')) {echo "la cookie no esta vacia";}//comprueba si no esta vacia la cookie
Cookie::delAll(); //borra todas las cookies
0.2.1 se agrego "static" a los metodos