Home >Backend Development >PHP Tutorial >Analysis of CI cookie operation methods (based on helper class library), cookiehelper_PHP tutorial
This article describes the method of CI operating cookies. Share it with everyone for your reference, the details are as follows:
There are three methods for CI to operate cookies. Among them, Ci comes with 2. Among them, we mainly explain the last method of ci cookie, which is to use the helpers’ built-in class library. The operation method is as follows:
Introduce class library:
$this->load->helper('cookie');
Set cookies
set_cookie("cookie名称",$cookie信息,times过期时间);
How to get it
get_cookie("cookie名称");
Since many small pages need to set cookies, I have encapsulated a small method: as follows
I like to operate in the parent class that the class inherits, because the framework introduces a parent class, so I will define some public information as methods and put them in the parent class. Enter the code below:
Generate cookie
public function saveCookie($info,$time){ $this->load->helper('cookie'); set_cookie("userInfo",$info,$time); //userInfo:cookie名称。$info:要保存的cookie 。$time 设置保存期,即过期时间 }
Get cookie:
public function getCookie($info){ //$info实际就是形成,调用这个方法的时候,需要获取哪个cookie名称就在调用的时候输入cookie名称 $this->load->helper("cookie"); return get_cookie($info); }
Readers who are interested in more CodeIgniter related content can check out the special topics of this site: "codeigniter introductory tutorial", "CI (CodeIgniter) framework advanced tutorial", "php date and time usage summary", "php object-oriented program" Design introductory tutorial", "php string (string) usage summary", "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone’s PHP program design based on the CodeIgniter framework.