Error
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.
  • JHTMLicon not supported. File not found.

cookies copied

simple php5 cookie class
Thursday, 24 February 2011 20:33
hello this class allow you manage your cookies
copied from العاب

/**
#
# aCookie
# Implementacion del uso de cookies en Ariadna Framework
# Acceso: Publico estatico
#
**/

class cookie {

private static $_encrypt = false;
private static $_hash = 'ghH43a2mmA08';
private static $_domain = '';
private static $_secure = false;
private static $_expire = 120;
private static $_path = '/';

#httponly
private static $_httponly = true;

#instancia de la clase
private static $instance = null;

#evitar que sea clonada o implementada accidentalmente la clase
private function __clone() {}

private function __construct() {}

public function encrypt($encrypt = true, $hash = '') {
self::$_encrypt = $encrypt;
if ($hash!='') {
self::$_hash = $hash;
}
}

public function secure($sec = true) {
self::$_secure = $sec;
}

public function expire($exp = 120) {
self::$_expire = $exp;
}

#getInstance, crea o accede a una instancia unica del Singleton
public static function getInstance() {
if(empty(self::$instance)) {
#crea una nueva instancia de la clase
self::$instance = new self();
}
return self::$instance;
}

public static function init() {
self::$_path = dirname($_SERVER['SCRIPT_FILENAME']);
self::$_domain = $_SERVER['SERVER_NAME'];
}

//manejo de cookies
public function __set($name, $value) {
self::set($name, $value);
}

public function __get($name) {
return self::get($name);
}

public function __isset($name) {
return isset($_COOKIE[$name]);
}

public function __unset($name) {
setcookie($name, null, time() - (3600 * 24 * 7), '', '', self::$_secure);
}

#definicion de una cookie
public static function set($name, $value, $expires='') {
#cifrado de cookies
if (self::$_encrypt) {
$val = base64_encode($value);
}
#vencimiento de la cookie
if ($expires=='') {
$vence = 60*self::$_expire;
} else {
$vence = 60*$expires;
}
//setcookie($name, $val, $vence, self::$cookie_path, , self::$cookie_secure);
if (!setcookie($name, $val, time()+$vence, '', '', self::$_secure)) {
trigger_error("cookie: cookie not created : {$name}");
return false;
}
}

#obtener el valor de una cookie:
public static function get($name) {
if (self::exists($name)) {
$value = $_COOKIE[$name];
} else {
return null;
}
#verifico si estoy cifrando las cookies:
if(self::$_encrypt) {
$val = base64_decode($value);
} else {
$val = $value;
}
return $val;
}


#verificar si existe la cookie:
public static function exists($name) {
if (isset($_COOKIE)) {
return isset($_COOKIE[$name]);
} else {
return false;
}
}

#borrar una cookie
public static function delete($name, $path = null, $domain = null, $httponly = true) {
if (self::exists($name)) {
return setcookie($name, null, time()-3600, $path, $domain, self::$_secure, $httponly);
} else {
return false;
}
}

}



thanks

Read more: http://feeds.dzone.com/~r/dzone/snippets/~3/a2nC1N1gjhA/12971

 


Taxonomy by Zaragoza Online