Advertisement
fabi0

Untitled

May 26th, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.15 KB | None | 0 0
  1. <?php
  2.  
  3. /*
  4.  * To change this license header, choose License Headers in Project Properties.
  5.  * To change this template file, choose Tools | Templates
  6.  * and open the template in the editor.
  7.  */
  8.  
  9. /**
  10.  * Description of Cookies
  11.  *
  12.  * @author fabi0
  13.  */
  14.  
  15. namespace Models;
  16.  
  17. final class Cookies {
  18.  
  19.     const Session = null;
  20.     const OneDay = 86400;
  21.     const SevenDays = 604800;
  22.     const ThirtyDays = 2592000;
  23.     const SixMonths = 15811200;
  24.     const OneYear = 31536000;
  25.     const Lifetime = -1;
  26.  
  27.     public function __construct() {
  28.        
  29.     }
  30.  
  31.     /**
  32.      * Returns true if there is a cookie with this name.
  33.      *
  34.      * @param string $name
  35.      * @return bool
  36.      */
  37.     public function exists($name) {
  38.         return isset($_COOKIE[$name]);
  39.     }
  40.  
  41.     /**
  42.      * Returns true if there no cookie with this name or it's empty, or 0,
  43.      * or a few other things. Check http://php.net/empty for a full list.
  44.      *
  45.      * @param string $name
  46.      * @return bool
  47.      */
  48.     public function isEmpty($name) {
  49.         return empty($_COOKIE[$name]);
  50.     }
  51.  
  52.     /**
  53.      * Get the value of the given cookie. If the cookie does not exist the value
  54.      * of $default will be returned.
  55.      *
  56.      * @param string $name
  57.      * @param string $default
  58.      * @return mixed
  59.      */
  60.     public function get($name, $default = '') {
  61.         return (isset($_COOKIE[$name]) ? $_COOKIE[$name] : $default);
  62.     }
  63.  
  64.     /**
  65.      * Set a cookie. Silently does nothing if headers have already been sent.
  66.      *
  67.      * @param string $name
  68.      * @param string $value
  69.      * @param mixed $expiry
  70.      * @param string $path
  71.      * @param string $domain
  72.      * @return bool
  73.      */
  74.     public function set($name, $value, $expiry = self::OneYear, $path = '/', $domain = "", $secure = false, $httpOnly = true) {
  75.         $retval = false;
  76.         if (!headers_sent()) {
  77.             if ($domain === false) {
  78.                 $domain = $_SERVER['HTTP_HOST'];
  79.             }
  80.  
  81.             if ($expiry === -1) {
  82.                 $expiry = 1893456000;
  83.             } elseif (is_numeric($expiry)) {
  84.                 $expiry += time();
  85.             } else {
  86.                 $expiry = strtotime($expiry);
  87.             }
  88.  
  89.             $retval = setcookie($name, $value, $expiry, $path, $domain, $secure, $httpOnly);
  90.  
  91.             if ($retval) {
  92.                 $_COOKIE[$name] = $value;
  93.             }
  94.         }
  95.         return $retval;
  96.     }
  97.  
  98.     /**
  99.      * Delete a cookie.
  100.      *
  101.      * @param string $name
  102.      * @param string $path
  103.      * @param string $domain
  104.      * @param bool $remove_from_global Set to true to remove this cookie from this request.
  105.      * @return bool
  106.      */
  107.     public function delete($name, $path = '/', $domain = false, $remove_from_global = false) {
  108.         $retval = false;
  109.         if (!headers_sent()) {
  110.             if ($domain === false) {
  111.                 $domain = $_SERVER['HTTP_HOST'];
  112.             }
  113.             $retval = setcookie($name, '', time() - 3600, $path, $domain);
  114.  
  115.             if ($remove_from_global) {
  116.                 unset($_COOKIE[$name]);
  117.             }
  118.         }
  119.         return $retval;
  120.     }
  121.  
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement