Advertisement
fabi0

Untitled

Nov 7th, 2013
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 KB | None | 0 0
  1. <?php
  2.  
  3. namespace Models;
  4.  
  5. final class Cookie {
  6.  
  7.     const Session = null;
  8.     const OneDay = 86400;
  9.     const SevenDays = 604800;
  10.     const ThirtyDays = 2592000;
  11.     const SixMonths = 15811200;
  12.     const OneYear = 31536000;
  13.     const Lifetime = -1;
  14.  
  15.     private function __construct() {
  16.        
  17.     }
  18.  
  19.     public static function exists($name) {
  20.         return isset($_COOKIE[$name]);
  21.     }
  22.  
  23.     public static function isEmpty($name) {
  24.         return empty($_COOKIE[$name]);
  25.     }
  26.  
  27.     public static function get($name, $default = '') {
  28.         return (isset($_COOKIE[$name]) ? $_COOKIE[$name] : $default);
  29.     }
  30.  
  31.     public static function set($name, $value, $expiry = self::OneYear, $path = '/', $domain = false) {
  32.         $retval = false;
  33.         if (!headers_sent()) {
  34.             if ($domain === false)
  35.                 $domain = $_SERVER['HTTP_HOST'];
  36.  
  37.             if ($expiry === -1)
  38.                 $expiry = 1893456000;
  39.             elseif (is_numeric($expiry))
  40.                 $expiry += time();
  41.             else
  42.                 $expiry = strtotime($expiry);
  43.  
  44.             $retval = @setcookie($name, $value, $expiry, $path, $domain);
  45.             if ($retval)
  46.                 $_COOKIE[$name] = $value;
  47.         }
  48.         return $retval;
  49.     }
  50.  
  51.     public static function delete($name, $path = '/', $domain = false, $remove_from_global = false) {
  52.         $retval = false;
  53.         if (!headers_sent()) {
  54.             if ($domain === false)
  55.                 $domain = $_SERVER['HTTP_HOST'];
  56.             $retval = setcookie($name, '', time() - 3600, $path, $domain);
  57.  
  58.             if ($remove_from_global)
  59.                 unset($_COOKIE[$name]);
  60.         }
  61.         return $retval;
  62.     }
  63.  
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement