This article analyzes the usage of cookie method in thinkphp3.x with examples. Share it with everyone for your reference, the details are as follows:
1. The cookie function is also a diversified operation function that completes the setting, acquisition and deletion of cookies.
Cookie is used for cookie setting, retrieval, and deletion operations:
Usage:
cookie($name, $value='', $option=null)
Parameters:
name (required): cookie variable to be operated
value (optional): cookie value to be set
option (optional): Incoming cookie setting parameters, empty by default
Return value See details (returns different values according to specific usage)
2. Cookie settings
cookie('name','value'); //设置cookie cookie('name','value',3600); // 指定cookie保存时间
Starting from version 3.1, the cookie method adds support for arrays (saved in lightweight json encoding format to reduce storage space), for example:
cookie('name',array('name1','name2'));
You can also complete complex cookie assignments by passing in parameters. The following is to set the validity period of 3600 seconds for the cookie value, and add the cookie prefix think_
cookie('name','value',array('expire'=>3600,'prefix'=>'think_'))
Array parameters can take the form of query parameters
cookie('name','value','expire=3600&prefix=think_')
Equivalent to the above usage.
The option parameter passed in supports the four index parameters of prefix, expire, path and domain. If no or null value is passed in, the four configuration parameters COOKIE_PREFIX, COOKIE_EXPIRE, COOKIE_PATH and COOKIE_DOMAIN will be taken by default. If only individual parameters are passed in, they will also be merged with the default configuration parameters.
3. Cookie acquisition
Getting cookies is very simple. No matter how you set the cookie, you only need to use:
$value = cookie('name');
If no cookie prefix is set, it is equivalent to
$value = $_COOKIE['name']
If the cookie prefix is set, it is equivalent to
$value = $_COOKIE['前缀+name']
4. Cookie deletion
To delete the value of a cookie, use:
cookie('name',null);
To delete all cookie values, you can use
cookie(null); // 清空当前设定前缀的所有cookie值 cookie(null,'think_'); // 清空指定前缀的所有cookie值
PS: Here are several formatting and beautification tools recommended for this site. I believe everyone can use them in future development:
php code online formatting and beautification tool:
http://tools.jb51.net/code/phpformat
JavaScript code beautification/compression/formatting/encryption tool:
http://tools.jb51.net/code/jscompress
Online XML formatting/compression tool:
http://tools.jb51.net/code/xmlformat
JSON code formatting and beautification tool:
http://tools.jb51.net/code/json
Online XML/JSON conversion tool:
http://tools.jb51.net/code/xmljson
SQL code online formatting and beautification tool:
http://tools.jb51.net/code/sqlcodeformat
Readers who are interested in more thinkPHP-related content can check out the special topics on this site: "Introduction to ThinkPHP Tutorial", "Summary of Common Methods of ThinkPHP", "Summary of Cookie Usage in PHP", "Basic Tutorial for Getting Started with Smarty Templates" and "PHP Template technology summary".
I hope this article will be helpful to everyone’s PHP programming based on the ThinkPHP framework.