LotusPhp notes: Cookie component usage details _ php instances

Source: Internet
Author: User
The most common operations of cookies are read, write, and delete operations. Of course, security operations are required. Otherwise, you can view and modify cookies maliciously, the LotusPhp Cookie component is also very easy to use.

First, you need to create a configuration file named cookie. conf. php. where to put it? when talking about the Config component, we will explain 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, and the Cookie content is automatically encrypted in the program. Of course, there is a drawback that the client cannot directly read and operate, you can only operate on servers. If you want to directly use js to operate the Cookie on the client, it is best not to use the LotusPhp Cookie component.

The key can be any character. The configuration file content is as follows:

The Code is as follows:


$ Config ['cookie. secret_key '] = 'sdfs445e22 $ @ % T ';


The usage of components is as follows:

The Code is as follows:


// Declare the Cookie object in singleton Mode
$ Cookie = LtObjectUtil: singleton ('ltcookies ');

// Or declare the Cookie object in the conventional way
// $ Cookie = new LtCookie ();
// $ Cookie-> init ();

/*
* When writing a Cookie, the method for setting the Cookie is actually the same as the setcookie built in php.
* $ Name Cookie name, required
* $ Value Cookie value, which can be a string or an array
* $ Expire expiration time, which is a standard Unix time mark. It can be obtained using the time () or mktime () functions, in seconds. Optional
* $ Path: Cookie path, optional
* $ Domain Cookie domain name (Optional). If multiple second-level domain names share cookies, set it to the root domain name.
* $ Secure indicates whether the Cookie is transmitted over the network through the encrypted HTTPS protocol. The default value is 0, indicating that the HTTPS protocol is not used. If yes, change it to 1.
* Method: $ cookie-> setCookie ($ name, $ value = '', $ expire = null, $ path = '/', $ domain = null, $ secure = 0 );

* Example: the userName value is 'I am Handsome body', valid for an hour, path is root directory, domain name is myDomain.com, not transmitted under HTTPS
* $ Cookie-> setCookie ('username', 'I am Handsome Kid', time () + 3600,'/', 'mydomain. com', 0 );
*/
$ Cookie-> setCookie ('username', 'I am Handsome kid ');

/*
* Read Cookie
* $ Name Cookie name, required
* Method: $ cookie-> getCookie ($ name );
* If the Cookie value exists, the return value is returned. If the Cookie value does not exist, null is returned.
*/
$ Cookie-> getCookie ('username ');

/*
* Delete a Cookie
* $ Name Cookie name, required
* $ Path: Cookie path, optional
* $ Domain Cookie domain name (Optional). If multiple second-level domain names share cookies, set it to the root domain name.
* Method: $ cookie-> delCookie ($ name, $ path = '/', $ domain = null)
*/
$ Cookie-> delCookie ('username ');


Finally, we have attached the php Cookie operation article. You can check that LotusPhp sets cookies the same as Php sets cookies.

How to Set, use, and delete cookies in PHP

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.