Detailed explanation of cookies for PHP session control
1. What is a cookie:
Sometimes also used in its plural form, Cookies refers to the data (usually encrypted) stored on the user's local terminal by some websites in order to identify the user's identity and perform session tracking. The most typical application of cookies is to determine whether a registered user has logged in to the website. The user may be prompted whether to retain user information the next time he enters the website to simplify the login procedure. These are the functions of cookies. Another important application is "shopping cart" processing. Users may choose different products on different pages of the same website within a period of time, and this information will be written to Cookies so that the information can be retrieved when making the final payment.
Advantages:
Good compatibility
Disadvantages:
1. Increased network traffic;
2. The data capacity is limited and can only store up to 4KB of data, which varies between browsers; the client can disable or clear cookies, thus affecting the functionality of the program.
3. It is unsafe. When multiple people share a computer, using cookies may leak user privacy and cause security issues.
2. Cookie working principle:
Cookie is a piece of text stored on the user's hard disk by the Web server, which stores some "key-value" pairs. Each Web site can store cookies on the user's machine and retrieve cookie data when needed. Usually Web sites have a cookie file. Every time the user visits site A, he will look for the cookie file of site A. If it exists, the username and password "key-value" pair data will be read from it. If the username and password "key-value" pair data is found, it is sent to site A together with the access request. If site A also receives the username and password "key-value" data when receiving the access request, it will use the username and password data to log in, so that the user does not need to enter the username and password. If the username and password "key-value" pair data is not received, it means that the user has not successfully logged in before. At this time, site A returns the login page to the user. In addition, each cookie has an expiration date, and cookies that have expired can no longer be used. Commonly used cookie operations are setting cookie data, reading cookie data, and deleting specified cookie data.
Syntax:
bool setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $path = "" [, string $domain = " " [, bool $secure = false [, bool $httponly = false ]]]]]] )
setcookie() defines the cookie and will be sent to the client together with the remaining HTTP headers . Like other HTTP headers, cookies must be sent before the script can produce any output (due to protocol limitations). Please call this function before producing any output (including and or spaces). Once the cookie is set, it can be read using $_COOKIE the next time the page is opened. Cookie values also exist in $_REQUEST
name: Cookie name.
value: Cookie value. This value is stored on the user's computer. Do not store sensitive information. For example, name is ‘cookiename’, and its value can be obtained through $_COOKIE[‘cookiename’].
expire: Cookie expiration time. This is a Unix timestamp, the number of seconds since the Unix epoch (January 1, 1970 00:00:00 GMT). In other words, you can basically use the result of the time() function plus the number of seconds you want to expire. Or you can use mktime(). time()+60*60*24*30 is to set the cookie to expire after 30 days. If set to zero, or if the parameter is omitted, the cookie will expire at the end of the session (i.e. when the browser is closed).
path: Cookie valid server path. When set to ‘/’, the cookie is valid for the entire domain name. If set to ‘/foo/’, the cookie is only valid for the /foo/ directory and its subdirectories in the domain (such as /foo/bar/). The default value is the current directory when the cookie is set.
domain: Valid domain name/subdomain name of the cookie. Setting it to a subdomain (e.g. ‘www.example.com’) will make the cookie valid for this subdomain and its third-level domain (e.g. w2.www.example.com). To make a cookie valid for an entire domain (including all its subdomains), just set it to the domain name (in this case, ‘example.com’).
secure: Set whether this cookie is only passed to the client through secure HTTPS connections. When set to TRUE, the cookie will only be set if a secure connection exists. If this requirement is handled on the server side, programmers need to only send such cookies over secure connections (as determined by $_SERVER["HTTPS"]).
httponly: Set to TRUE, the cookie can only be accessed through the HTTP protocol. This means that cookies cannot be accessed through scripting languages such as JavaScript. FALSE, there is no limit.
Return value
If output is generated before calling this function, setcookie() will fail and return FALSE. Returns TRUE if setcookie() runs successfully. Of course, it does not mean whether the user has accepted cookies.
Setting and reading cookies
1 2 3 4 5 6 7 8 9 |
|
Deleting cookies
To delete a cookie, the expiration time should be set to the past to trigger the browser's deletion mechanism.
?>
用于记录当前用户访问网站的次数:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
用户验证身份是验证cookie:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
|
相关推荐:
The above is the detailed content of Detailed explanation of cookies for PHP session control. For more information, please follow other related articles on the PHP Chinese website!

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

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

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











PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7
