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
- Cookie ('name', 'value'); // sets the cookie
- 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-]
- 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 _
- Cookie ('name', 'value', array ('expire '=> 3600, 'prefix' => 'think _'))
You can copy the code array parameters in the query format.
- 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:
- $ Value = cookie ('name ');
If no cookie prefix is set for the copied code, it is equivalent
- $ Value = $ _ COOKIE ['name']
If the cookie prefix is set
- $ Value = $ _ COOKIE ['Prefix + name']
Copy the code Cookie to delete the value of a cookie. use:
- Cookie ('name', null );
Copy the code to delete all Cookie values, you can use
- Cookie (null); // clear all cookie values with the specified prefix
- Cookie (null, 'think _ '); // clear all cookie values with the specified prefix