Table of Contents
PHP Cookie processing function, phpcookie function
Home Backend Development PHP Tutorial PHP Cookie processing function, phpcookie function_PHP tutorial

PHP Cookie processing function, phpcookie function_PHP tutorial

Jul 12, 2016 am 08:51 AM
cookie

(o゜▽゜)o☆[BINGO!]

ok, let’s first take a look at what cookies are?

A cookie is a small file left by the server on the client to identify the user or store some data (note that the session is stored on the server side, which is one of the differences between the two). When we usually log in to a portal, there will be an option to "save login" or "automatically log in next time". When we check it, the server will create a cookie file in our browser to save our information. Whenever a computer requests a page through the browser, it sends a cookie at the same time. With PHP, cookie values ​​can be created and retrieved. Cookies play a very important role in the web. Cookies were created as early as the Netscape browser. Cookies are often used in user authentication systems.

1. Create cookie

The function setcookie() can generate cookies in PHP. Since cookies are the content of the HTTP header part, must call setcookie() before outputting any data. This restriction is similar to header(), defined:

1 bool setcookie( string name[,string value][,int expire][,string path][,string domain][,bool secure][,bool httponly])

So many parameters! Σ(⊙▽⊙"a Don’t worry, let’s take a look at the functions of each parameter:

name : Required, indicating the name of the cookie.

value: Optional, indicating the cookie value, stored in the client. When empty, it means revoking the cookie information in the client (this way the cookie can be deleted).

expire: Optional, indicating the validity deadline of the cookie, that is, the expiration time. If it is not specified or specified as 0, it usually expires when the browser is closed.

path: Optional, valid cookie path.

domain: Optional, cookie valid domain name.

secure: Indicates that it is only valid when HTTPS secure transmission.

Example (create a cookie with name test and value China):

<span>1</span> <?<span>php
</span><span>2</span> <span>setcookie</span>("test",China"<span>);
</span><span>3</span> ?>
Copy after login

The value given by setcookie() can only be a number or a string , and cannot be other complex structures.

2. Get cookies

After creating the cookie, you can use the predefined variable $_COOKIE to obtain the cookie. However, cookies can only be obtained on other pages, because in php, the cookie set by will not take effect on this page unless the page is refreshed .

Example:

<span>1</span> <?<span>php
</span><span>2</span> <span>setcookie</span>("test","China"<span>);
</span><span>3</span> <span>echo</span> "cookie is ".<span>$_COOKIE</span>["test"<span>];</span>
4 ?>
Copy after login

The reason why the page needs to be refreshed here is because the cookie value will not be stored in the $_COOKIE variable immediately after calling setcookie(), but will be sent to the server with the http request, and then the cookie value will appear. in $_COOKIE.

3. Cookie validity period

Cookies have a life cycle, which is how long the cookie is valid. A third parameter can be set to set the validity time.

Examples (several ways to set cookie validity time):

<span>1</span> <span>setcookie</span>("cookie_one","A",<span>time</span>()+60*60);       <span>//</span><span>cookie在一小时后失效</span>
<span>2</span> <span>setcookie</span>("cookie_two","B",<span>time</span>()+60*60*24);    <span>//</span><span>cookie在一天后失效</span>
<span>3</span> <span>setcookie</span>("cookie_three","C",<span>mktime</span>(23,53,19,10,09,2020));    <span>//</span><span>cookie在2020年10月9日23时53分19秒失效</span>
<span>4</span> <span>setcookie</span>("cookie_four","D");     <span>//</span><span>关闭浏览器后cookie失效   </span>
Copy after login

4. Valid path of cookie

The path in the cookie is used to control the path in which the set cookie is valid . The default is '/', which is valid in all paths , that is, it is valid in the entire server domain name. When set After setting other paths, it will only be valid under the set path and sub-path, for example:

<span>1</span> <span>setcookie</span>('test', <span>time</span>(), 0, '/path');
Copy after login

The above settings will make test valid under /path and the subpath /path/abc, but the cookie value of test cannot be read in the root directory.

In general, all paths are used. Only in rare cases when there are special needs, the path will be set. In this case, the cookie value will only be passed in the specified path, which can save data transmission. Enhance security and improve performance.

5. Delete cookies

Deleting cookies is relatively simple and is also achieved through setcookie() (do not use unset()!!!) The following code is a simple example:

<span>1</span> <span>setcookie</span>("test","");
Copy after login

Delete cookies by setting the second parameter to empty . If you provide specific values ​​for the cookie when you set it, you still need to provide those parameters when you delete the cookie so that PHP can delete the cookie correctly.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1132413.htmlTechArticlePHP Cookie processing function, phpcookie function (o゜▽゜)o☆[BINGO!] ok, let’s look at it first What are cookies? Cookies are left by the server on the client to identify the user or store some...
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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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 Fix Roblox 403 Forbidden Error on Google Chrome How to Fix Roblox 403 Forbidden Error on Google Chrome May 19, 2023 pm 01:49 PM

Many Windows users have recently encountered an unusual error called Roblox403 Forbidden Error while trying to access website URLs in Google Chrome browser. Even after restarting the Chrome app multiple times, they were unable to do anything. There could be several potential causes for this error, some of which we've outlined and listed below. Browsing history and other cache of Chrome and corrupted data Unstable internet connection Incorrect website URLs Extensions installed from third-party sources After considering all the above aspects, we have come up with some fixes that can help users resolve this issue. If you encounter the same problem, check out the solutions in this article. Fix 1

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.

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

How cookies work How cookies work Sep 20, 2023 pm 05:57 PM

The working principle of cookies involves the server sending cookies, the browser storing cookies, and the browser processing and storing cookies. Detailed introduction: 1. The server sends a cookie, and the server sends an HTTP response header containing the cookie to the browser. This cookie contains some information, such as the user's identity authentication, preferences, or shopping cart contents. After the browser receives this cookie, it will be stored on the user's computer; 2. The browser stores cookies, etc.

Does clearing cookies have any impact? Does clearing cookies have any impact? Sep 20, 2023 pm 06:01 PM

The effects of clearing cookies include resetting personalization settings and preferences, affecting ad experience, and destroying login status and password remembering functions. Detailed introduction: 1. Reset personalized settings and preferences. If cookies are cleared, the shopping cart will be reset to empty and products need to be re-added. Clearing cookies will also cause the login status on social media platforms to be lost, requiring re-adding. Enter your username and password; 2. It affects the advertising experience. If cookies are cleared, the website will not be able to understand our interests and preferences, and will display irrelevant ads, etc.

Detailed explanation of where browser cookies are stored Detailed explanation of where browser cookies are stored Jan 19, 2024 am 09:15 AM

With the popularity of the Internet, we use browsers to surf the Internet have become a way of life. In the daily use of browsers, we often encounter situations where we need to enter account passwords, such as online shopping, social networking, emails, etc. This information needs to be recorded by the browser so that it does not need to be entered again the next time you visit. This is when cookies come in handy. What are cookies? Cookie refers to a small data file sent by the server to the user's browser and stored locally. It contains user behavior of some websites.

What are the dangers of cookie leakage? What are the dangers of cookie leakage? Sep 20, 2023 pm 05:53 PM

The dangers of cookie leakage include theft of personal identity information, tracking of personal online behavior, and account theft. Detailed introduction: 1. Personal identity information is stolen, such as name, email address, phone number, etc. This information may be used by criminals to carry out identity theft, fraud and other illegal activities; 2. Personal online behavior is tracked and analyzed through cookies With the data in the account, criminals can learn about the user's browsing history, shopping preferences, hobbies, etc.; 3. The account is stolen, bypassing login verification, directly accessing the user's account, etc.

See all articles