This article mainly introduces the CI method for cookie operation, and analyzes the related skills of using the cookie class library of the helper class library in the form of examples, for more information, see the following example. We will share this with you for your reference. The details are as follows:
There are three methods for CI to operate on cookies, which are included in Ci 2. here we will mainly explain the last method of ci cookie, that is, using the helpers built-in class library. The operation method is as follows:
Introduce the class library:
$this->load->helper('cookie');
Set cookie
Set_cookie ("cookie name", $ cookie information, times expiration time );
Obtaining method
Get_cookie ("cookie name ");
Because many small pages require cookie settings, I encapsulate the following small method:
I like to operate in the parent class inherited by the class, because the framework introduces a parent class, so I will define some common information as a method and put it in the parent class. Code entry:
Generate cookie
Public function saveCookie ($ info, $ time) {$ this-> load-> helper ('cookier'); set_cookie ("userInfo", $ info, $ time ); // userInfo: cookie name. $ Info: the cookie to save. $ Time: Set the retention period, that is, the expiration time}
Get cookie:
Public function getCookie ($ info) {// $ info is actually formed. when this method is called, enter the cookie name $ this-> load-> helper ("cookie"); return get_cookie ($ info );}