Home > Backend Development > PHP Tutorial > Usage analysis of cookie method in thinkphp3.x, thinkphp3.xcookie_PHP tutorial

Usage analysis of cookie method in thinkphp3.x, thinkphp3.xcookie_PHP tutorial

WBOY
Release: 2016-07-12 08:52:21
Original
1049 people have browsed it

Usage analysis of cookie method in thinkphp3.x, thinkphp3.xcookie

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)
Copy after login

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保存时间

Copy after login

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'));

Copy after login

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_'))

Copy after login

Array parameters can take the form of query parameters

cookie('name','value','expire=3600&prefix=think_')

Copy after login

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');

Copy after login

If no cookie prefix is ​​set, it is equivalent to

$value = $_COOKIE['name']

Copy after login

If the cookie prefix is ​​set, it is equivalent to

$value = $_COOKIE['前缀+name']

Copy after login

4. Cookie deletion

To delete the value of a cookie, use:

cookie('name',null);

Copy after login

To delete all cookie values, you can use

cookie(null); // 清空当前设定前缀的所有cookie值
cookie(null,'think_'); // 清空指定前缀的所有cookie值

Copy after login

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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1127837.htmlTechArticleUsage analysis of the cookie method in thinkphp3.x, thinkphp3.xcookie This article analyzes the cookie method in thinkphp3.x with an example usage. Share it with everyone for your reference, the details are as follows: 1. cook...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template