Often use Yii 1.1,yii operation cookie notation, can refer to http://www.yiiframework.com/wiki/152/cookie-management-in-yii/, described in this article, just carry out the encapsulation, convenient to call.
Words do not say much, on the code cookie.php, please put in/protected/components under the convenient call.
// +----------------------------------------------------------------------// | Create date:2015.12.31//+----------------------------------------------------------------------final class Cookie {/** * Set Cookie * @param string $name name * @param string $value value * @param int $expire time in seconds * @param the array $options option, refer to Chttpcookie * @return Boolean */static public function set ($name, $value = ", $expire = 0, $options = []) {if (!is_string ($name) | |!is_int ($expire) | |!is_array ($options)) {return False } $cookie = new Chttpcookie ($name, $value); if ($expire) {$cookie->expire = time () + $expire; } if (!empty ($options)) {foreach ($options as $optionIndex = = $optionVal) {$cookie-& gt; $optionIndex = $optionVal; }} Yii::app ()->getrequest ()->cookies[$name] = $cookie; return true; }/** * Get Cookie * @param string $name name * @return Mixed */static public function get ($name) {if (! Is_string ($name) && $name)) {return false; } return Isset (Yii::app ()->getrequest ()->cookies[$name])? Yii::app ()->getrequest ()->cookies[$name]->value: "; }/** * Clear All Cookies * @return void */static public Function clear () {Yii::app ()->getrequest ( )->getcookies ()->clear (); }/** * Delete a Cookie * @param string $name name * @return Boolean */static public function remove ($nam e) {if (!) ( Is_string ($name) && $name) | | !isset (Yii::app ()->getrequest ()->cookies[$name])) {return false; } self::set ($name, ',-1); return true; }}
Call Method:
Set cookies
Cookie::set (' name ', ' Lee ', 60);
Get cookies
Cookie::get (' name ');
Delete a cookie
Cookie::remove (' name ');
Clear All Cookies
Cookie::clear ();
Disclaimer: This article is original, reproduced please indicate the source at the beginning!!!
The above is the introduction of DRY Goods!!! Yii 11 operates cookies, including aspects of the content, hoping to be of interest to the PHP tutorial friends helpful.