The usage analysis of the cookie method in thinkphp3.x, thinkphp3.xcookie. The usage of the cookie method in thinkphp3.x, thinkphp3.xcookie This article analyzes the usage of the cookie method in thinkphp3.x. I. usage analysis of cookie method in cook thinkphp3.x, thinkphp3.xcookie
This document analyzes the usage of the cookie method in thinkphp3.x. We will share this with you for your reference. The details are as follows:
1. the cookie function is also a diverse operation function that completes cookie setting, acquisition, and deletion.
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.
For details about the returned values, see (return different values based on specific usage)
II. Cookie settings
Cookie ('name', 'value'); // Set cookiecookie ('name', 'value', 3600); // specify the cookie retention time
Since version 3.1, the cookie method has added support for arrays (using lightweight json encoding format to save and reduce storage space). For example:
cookie('name',array('name1','name2'));
You can also assign values to complex cookies by passing in parameters. The following describes how to set the 3600-second validity period for the cookie value and add the cookie prefix think _
cookie('name','value',array('expire'=>3600,'prefix'=>'think_'))
The array parameters can be in the query format.
cookie('name','value','expire=3600&prefix=think_')
It 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.
3. Cookie acquisition
It is very easy to get the cookie. no matter how you set the cookie, you only need to use:
$value = cookie('name');
If the cookie prefix is not set, it is equivalent
$value = $_COOKIE['name']
If the cookie prefix is set, it is equivalent
$ Value = $ _ COOKIE ['Prefix + name']
IV. delete cookies
Delete the value of a cookie. use:
cookie('name',null);
To delete all Cookie values, you can use
Cookie (null); // clear all cookie values with the specified prefix (null, 'think _ '); // clear all cookie values with the specified prefix
PS: We recommend several formatting and beautification tools on this site. I believe you can use them in future development:
Php code online formatting and beautification tools:
Http://tools.jb51.net/code/phpformat
JavaScript code beautification/compression/Formatting/encryption tools:
Http://tools.jb51.net/code/jscompress
Online XML formatting/compression tools:
Http://tools.jb51.net/code/xmlformat
JSON code formatting and beautification tools:
Http://tools.jb51.net/code/json
Online XML/JSON conversion tools:
Http://tools.jb51.net/code/xmljson
SQL code online formatting and beautification tools:
Http://tools.jb51.net/code/sqlcodeformat