Home >Backend Development >PHP Tutorial >LotusPhp Notes: Detailed Explanation of the Use of Cookie Component_PHP Tutorial
LotusPhp’s Cookie component is also very simple and easy to use.
First of all, you need to create a new configuration file. The file name is cookie.conf.php. As for where to put it, I will explain it when I talk about the Config component. Today I will talk about how to use it and what steps are required.
The main content of the cookie configuration file is to define the encryption key of the cookie. The program automatically encrypts the cookie content. Of course, this has a drawback, that is, the client cannot directly read and operate it, and can only be operated by the server. If you want to directly use js to operate Cookie on the client, it is best not to use LotusPhp's Cookie component.
The key can be any character. The content of the configuration file is as follows:
// Or declare the Cookie object in the usual way
// $cookie = new LtCookie();
// $cookie->init();
/*
* Write Cookie. The method of setting Cookie is actually the same as PHP's built-in setcookie
* $name Cookie name, required
* $value Cookie value, can be The string can be an array
* $expire expiration time, which is a standard Unix time stamp, which can be obtained using the time() or mktime() function, in seconds, optional
* $path Cookie path, Optional
* $domain Cookie domain name, optional. If cookies are shared between multiple second-level domain names, just set it as the root domain name
* $secure parameter indicates whether this cookie is on the network through the encrypted HTTPS protocol Upload transmission, the default value is 0, which means the HTTPS protocol is not used, if so, change it to 1
* Method: $cookie->setCookie($name, $value = '', $expire = null, $path = '/', $domain = null, $secure = 0);
* Example: userName value is 'I am a handsome guy', valid for one hour, path is root directory, domain name is myDomain.com, not transmitted under HTTPS
* $cookie->setCookie('userName', 'I am a handsome guy', time()+3600, '/', 'myDomain.com', 0);
*/
$cookie->setCookie('userName', 'I am a handsome guy') ;
/*
* Read Cookie
* $name Cookie name, required
* Method: $cookie->getCookie($name);
* If the Cookie value exists Return value, if it does not exist, return null
*/
$cookie->getCookie('userName');
/*
* Delete Cookie
* $name Cookie name, required
* $path Cookie path, optional
* $domain Cookie domain name, optional, if there are multiple To share cookies between level domain names, just set it as the root domain name
* Method: $cookie->delCookie($name, $path = '/', $domain = null)
*/
$cookie->delCookie('userName');