PHP setcookie usage
Definition and usage
The setcookie() function sends an HTTP cookie to the client.
A cookie is a variable sent by the server to the browser. Cookies are typically small text files that a server embeds on a user's computer. This cookie is sent each time the computer requests a page through the browser.
The name of the cookie is specified as a variable of the same name. For example, if the cookie being sent is named "name", a variable named $user is automatically created containing the cookie's value.
There cannot be any losses before assigning a value to the cookie. The function returns true if successful, false otherwise.
Note: Cookie settings must be refreshed before they can take effect.
Syntax
setcookie(name,value,expire,path,domain,secure)
Parameters | Description |
---|---|
name | required. Specifies the name of the cookie. |
value | Required. Specifies the value of the cookie. |
expire | Optional. Specifies the validity period of the cookie. |
path | optional. Specifies the server path for cookies. |
domain | Optional. Specifies the domain name for the cookie. |
secure | Optional. Specifies whether cookies are transmitted over a secure HTTPS connection. |
Tips and Notes
Note: The value of the cookie named "user" can be accessed through $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"].
Note: When sending a cookie, the cookie value is automatically URL encoded. URL decoding is done on reception. If you don't need this, you can use setrawcookie() instead.
Example 1
Set and send cookie:
<?php $value ="my cookie value"; // 发送一个简单的 cookie setcookie("TestCookie",$value); ?> <?php $value ="my cookie value"; // 发送一个 24 小时候过期的 cookie setcookie("TestCookie",$value, time()+3600*24); ?>
Example 2
Different ways to retrieve cookie value:
// 输出个别的 cookie echo $_COOKIE["TestCookie"]; echo " "; echo $HTTP_COOKIE_VARS["TestCookie"]; echo " "; // 输出所有 cookie print_r($_COOKIE); ?>
Output:
my cookie value my cookie value Array([TestCookie]=>my cookie value)
Example 3
Passed Set the expiration date to a date/time in the past, delete a cookie:
<?php // 把失效日期设置为一小时前 setcookie ("TestCookie","", time()-3600); ?>
Example 4
Create an array of cookies:
<?php setcookie("cookie[three]","cookiethree"); setcookie("cookie[two]","cookietwo"); setcookie("cookie[one]","cookieone"); // 输出 cookie (在重载页面后) if(isset($_COOKIE["cookie"])){ foreach($_COOKIE["cookie"]as $name => $value){ echo "$name : $value "; } } ?>
Output:
three : cookiethree two : cookietwo one : cookieone
Example 5
About setting cookies The problem does not take effect after. Usually the reason is that the scope is not set
<?php setcookie("a","bb",time()+3600,"/",".hi-docs.com"); // 注意把域名设置为自己的 ?>
The above introduces the usage of PHP setcookie, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.
Related articles:
If setcookie does not set the expiration time, how should I write the set path?
php setcookie function invalid
cookies setting PHP setcookie setting Cookie usage and setting invalid problem

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

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.

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.

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

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.

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.

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.

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.
