Instantly share code, notes, and snippets.
Created
November 17, 2010 19:05
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save Twipped/703846 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Chainable class used to simplify the action of setting and unsetting cookies. | |
* @access public | |
* @author cjoudrey, chipersoft | |
*/ | |
class Cookie | |
{ | |
/** | |
* @var string | |
*/ | |
private $name; | |
/** | |
* @var string | |
*/ | |
private $value; | |
/** | |
* @var int | |
*/ | |
private $expire = 0; | |
/** | |
* @var string | |
*/ | |
private $path; | |
/** | |
* @var string | |
*/ | |
private $domain; | |
/** | |
* @var bool | |
*/ | |
private $secure = false; | |
/** | |
* @var bool | |
*/ | |
private $httponly = false; | |
/** | |
* Constructor | |
* @param string $name | |
*/ | |
public function __construct($name) | |
{ | |
$this->name = $name; | |
$this->path = '/'; | |
} | |
/** | |
* Set the value of the cookie. | |
* @param string $value | |
* @return Cookie | |
*/ | |
public function & setValue($value) | |
{ | |
$this->value = $value; | |
return $this; | |
} | |
/** | |
* Set the expiration timestamp of the cookie. | |
* @param int $expire | |
* @return Cookie | |
*/ | |
public function & setExpire($expire) | |
{ | |
$this->expire = $expire; | |
return $this; | |
} | |
/** | |
* Set the duration of the cookie. | |
* This is the same as setting the expire to current time + duration | |
* @param int $duration | |
* @return Cookie | |
*/ | |
public function & setDuration($duration) | |
{ | |
$this->expire = time() + $duration; | |
return $this; | |
} | |
/** | |
* Set the path of the cookie. | |
* @param string $path | |
* @return Cookie | |
*/ | |
public function & setPath($path) | |
{ | |
$this->path = $path; | |
return $this; | |
} | |
/** | |
* Set the domain of the cookie | |
* @param string $domain | |
* @return Cookie | |
*/ | |
public function & setDomain($domain) | |
{ | |
$this->domain = $domain; | |
return $this; | |
} | |
/** | |
* Indicates that the cookie should only be transmitted over HTTPS. | |
* @param bool $secure | |
* @return Cookie | |
*/ | |
public function & setSecure($secure) | |
{ | |
$this->secure = $secure; | |
return $this; | |
} | |
/** | |
* Indicates that the cookie is only accessible through the HTTP protocol. | |
* For example, when TRUE, the cookie would not be available in Javascript through document.cookie. | |
* @param bool $httponly | |
* @return Cookie | |
*/ | |
public function & setHttponly($httponly) | |
{ | |
$this->httponly = $httponly; | |
return $this; | |
} | |
/** | |
* Set the cookie. | |
* @return void | |
*/ | |
public function set() | |
{ | |
setcookie($this->name, $this->value, $this->expire, $this->path, $this->domain, $this->secure, $this->httponly); | |
} | |
/** | |
* Empty the value and the expire of the cookie, essentially removing the cookie. | |
* @return void | |
*/ | |
public function remove() | |
{ | |
$this->setExpire(0)->setValue(null)->set(); | |
} | |
/** | |
* Cookie factory function. | |
* @return Cookie | |
*/ | |
public static function C($name) | |
{ | |
return new self($name); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment