Dry!!! Yii 11 Operation Cookie

Source: Internet
Author: User
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.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.