LotusPhp notes: details on the use of Cookie components
- 2020-06-01 08:58:10
- OfStack
The Cookie component of LotusPhp is also very simple to use.
First, create a new configuration file called cookie.conf.php, where to put it, and when we get to the Config component, we'll have an explanation of how to use it and what the steps are.
The main content of Cookie configuration file is to define the encryption key of Cookie, and the program automatically encrypts the content of Cookie. Of course, there is a disadvantage: the client cannot read and operate directly, but only the server can. If you are going to use js directly to work with Cookie on the client side, it is best not to use the Cookie component of LotusPhp.
The key can be any character. The configuration file reads as follows:
<?php
$config['cookie.secret_key'] = 'sdfs445e22$$$@%T';
Components are used as follows:
<?php
// Singleton pattern declaration Cookie object
$cookie = LtObjectUtil::singleton('LtCookie');
// Or declare it in the usual way Cookie object
// $cookie = new LtCookie();
// $cookie->init();
/*
* write Cookie , Set up the Cookie The method is actually sum php The built-in setcookie is 1 sample 1 The sample of
* $name Cookie Name, required
* $value Cookie It could be a string it could be an array
* $expire Expiration time, yes 1 A standard Unix Time stamp, you can use it time() or mktime() The function gets, in seconds
* $path Cookie Path, select
* $domain Cookie Domain name, optional, if more than one 2 Level domains are Shared between domains Cookie , just set it to the root domain
* $secure The parameter represents this Cookie Whether through encryption HTTPS The protocol is transmitted over the network and the default value is 0 "Is not used to represent HTTPS Agreement, if it is 1
* Methods: $cookie->setCookie($name, $value = '', $expire = null, $path = '/', $domain = null, $secure = 0);
* sample : userName A value of ' I am a handsome boy ' , valid for 1 Hour, the path is the root directory, the domain name is myDomain.com And do not HTTPS The transmission
* $cookie->setCookie('userName', ' I am a handsome boy ', time()+3600, '/', 'myDomain.com', 0);
*/
$cookie->setCookie('userName', ' I am a handsome boy ');
/*
* read Cookie
* $name Cookie Name, required
* Methods: $cookie->getCookie($name);
* if Cookie A value exists and returns a value, not a value null
*/
$cookie->getCookie('userName');
/*
* delete Cookie
* $name Cookie Name, required
* $path Cookie Path, select
* $domain Cookie Domain name, optional, if more than one 2 Level domains are Shared between domains Cookie , just set it to the root domain
* Methods: $cookie->delCookie($name, $path = '/', $domain = null)
*/
$cookie->delCookie('userName');
Finally, attached is the article about php operating Cookie. You can compare it. In fact, LotusPhp setting Cookie and Php setting Cookie are the same
Set, use, and delete the Cookie solution in PHP