<?php /* */ $_COOKIE['lang'] = 'ru';
<?php class CookieStorage implements ArrayAccess { // const DEFAULT_EXPIRE_TIME = 1411200; // 2 // private $_storage; // $_COOKIE public function __construct($cookies) { $this->_storage = $cookies; } // ArrayAccess public function offsetExists ($offset) { return isset ($this->_storage[$offset]); } // ArrayAccess public function offsetUnset ($offset) { unset($this->_storage[$offset]); } // public function offsetGet ($offset) { return $this->_storage[$offset]; } // public function offsetSet ($offset, $value) { if( $this->_setCookie($offset, $value) ){ $this->_storage[$offset] = $value; } else{ trigger_error('Cookie value was not set', E_USER_WARNING); } } // setcookie private function _setCookie( $name, $value, $expire = 0, $path = '/', $domain = false, $secure = false , $httponly = false ){ if (!headers_sent()){ if ($domain === false){ $domain = $_SERVER['HTTP_HOST']; } if( $expire == 0 ){ $expire = time() + self::DEFAULT_EXPIRE_TIME; } return setcookie ( $name, $value, $expire, $path, $domain, $secure, $httponly ); } return false; } } // $_COOKIE = new CookieStorage( $_COOKIE );
Source: https://habr.com/ru/post/134870/
All Articles