ThinkPHP functions: cookie method

Source: Internet
Author: User
The cookie function is also a diverse operation function that completes cookie setting, obtaining, and deleting operations. The cookie function is also a diverse operation function that completes cookie setting, obtaining, and deleting operations.
Cookies are used to set, retrieve, and delete cookies.
Usage Cookie ($ name, $ value = '', $ option = null)
Parameters Name (required): cookie variable to be operated
Value (optional): the cookie value to be set.
Option (optional): specifies the input cookie setting parameter. it is null by default.
Return value See details (return different values based on specific usage)
Cookie settings
  1. Cookie ('name', 'value'); // sets the cookie
  2. Cookie ('name', 'value', 3600); // specify the cookie retention time
Starting from Code 3.1, the cookie method adds support for arrays (using lightweight json encoding format to save and reduce storage space), for example: [-more-]
  1. Cookie ('name', array ('name1', 'name2 '));
The copy code also supports parameter input to complete complex cookie assignment. The following describes how to set a 3600-second validity period for the cookie value and add the cookie prefix think _
  1. Cookie ('name', 'value', array ('expire '=> 3600, 'prefix' => 'think _'))
You can copy the code array parameters in the query format.
  1. Cookie ('name', 'value', 'expire = 3600 & prefix = think _')
The copied code is equivalent to the above usage.
The input option parameter supports four index parameters: prefix, expire, path, and domain. If no index parameter is input or null value is input, COOKIE_PREFIX, COOKIE_EXPIRE, COOKIE_PATH, and COOKIE_DOMAIN are used by default. If only a few parameters are input, they are also combined with the default configuration parameters. It is very easy to obtain the Cookie. no matter how you set the cookie, you only need to use:
  1. $ Value = cookie ('name ');
If no cookie prefix is set for the copied code, it is equivalent
  1. $ Value = $ _ COOKIE ['name']
If the cookie prefix is set
  1. $ Value = $ _ COOKIE ['Prefix + name']
Copy the code Cookie to delete the value of a cookie. use:
  1. Cookie ('name', null );
Copy the code to delete all Cookie values, you can use
  1. Cookie (null); // clear all cookie values with the specified prefix
  2. Cookie (null, 'think _ '); // clear all cookie values with the specified prefix

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.