Table of Contents
Usage analysis of cookie method in thinkphp3.x, thinkphp3.xcookie
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

Jul 12, 2016 am 08:52 AM
cookie thinkphp

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...
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to run thinkphp project How to run thinkphp project Apr 09, 2024 pm 05:33 PM

To run the ThinkPHP project, you need to: install Composer; use Composer to create the project; enter the project directory and execute php bin/console serve; visit http://localhost:8000 to view the welcome page.

Where are the cookies on your computer? Where are the cookies on your computer? Dec 22, 2023 pm 03:46 PM

Cookies on your computer are stored in specific locations on your browser, depending on the browser and operating system used: 1. Google Chrome, stored in C:\Users\YourUsername\AppData\Local\Google\Chrome\User Data\Default \Cookies etc.

Where are cookies stored? Where are cookies stored? Dec 20, 2023 pm 03:07 PM

Cookies are usually stored in the cookie folder of the browser. Cookie files in the browser are usually stored in binary or SQLite format. If you open the cookie file directly, you may see some garbled or unreadable content, so it is best to use Use the cookie management interface provided by your browser to view and manage cookies.

There are several versions of thinkphp There are several versions of thinkphp Apr 09, 2024 pm 06:09 PM

ThinkPHP has multiple versions designed for different PHP versions. Major versions include 3.2, 5.0, 5.1, and 6.0, while minor versions are used to fix bugs and provide new features. The latest stable version is ThinkPHP 6.0.16. When choosing a version, consider the PHP version, feature requirements, and community support. It is recommended to use the latest stable version for best performance and support.

How to run thinkphp How to run thinkphp Apr 09, 2024 pm 05:39 PM

Steps to run ThinkPHP Framework locally: Download and unzip ThinkPHP Framework to a local directory. Create a virtual host (optional) pointing to the ThinkPHP root directory. Configure database connection parameters. Start the web server. Initialize the ThinkPHP application. Access the ThinkPHP application URL and run it.

How to install thinkphp How to install thinkphp Apr 09, 2024 pm 05:42 PM

ThinkPHP installation steps: Prepare PHP, Composer, and MySQL environments. Create projects using Composer. Install the ThinkPHP framework and dependencies. Configure database connection. Generate application code. Launch the application and visit http://localhost:8000.

Where are the mobile cookies? Where are the mobile cookies? Dec 22, 2023 pm 03:40 PM

Cookies on the mobile phone are stored in the browser application of the mobile device: 1. On iOS devices, Cookies are stored in Settings -> Safari -> Advanced -> Website Data of the Safari browser; 2. On Android devices, Cookies Stored in Settings -> Site settings -> Cookies of Chrome browser, etc.

Which one is better, laravel or thinkphp? Which one is better, laravel or thinkphp? Apr 09, 2024 pm 03:18 PM

Performance comparison of Laravel and ThinkPHP frameworks: ThinkPHP generally performs better than Laravel, focusing on optimization and caching. Laravel performs well, but for complex applications, ThinkPHP may be a better fit.

See all articles