New PHP Cookie Setting Method Revealed_PHP Tutorial
After a long period of development of PHP, many users are familiar with PHP. Here I will post about PHP Cookie settings. PHP uses the SetCookie function to set cookies. One thing that must be noted is that cookies are part of the HTTP protocol header and are used to transfer information between the browser and the server, so the Cookie function must be called before any content belonging to the HTML file itself is output. The SetCookie function defines a Cookie and appends it to the end of the HTTP header. The prototype of the SetCookie function is as follows:
<ol class="dp-xml"><li class="alt"><span><span>int SetCookie(string name, string value, int expire, string path, string domain, int secure); </span></span></li></ol>
The PHP Cookie settings on the same page are actually from back to front, so if you want to delete a new Cookie before inserting it, you must first write the insertion statement, and then write the deletion statement, otherwise Undesirable results may occur. Let’s look at a few examples of simple PHP cookie settings:
<ol class="dp-xml"><li class="alt"><span><span>SetCookie("MyCookie", "Value of MyCookie"); </span></span></li></ol>
With expiration time:
<ol class="dp-xml"><li class="alt"><span><span>SetCookie("WithExpire", "Expire in 1 hour", time()+3600);//3600秒=1小时 </span></span></li></ol>
Everything is available:
<ol class="dp-xml"><li class="alt"><span><span>SetCookie("FullCookie", "Full cookie value", time()+3600, "/forum", ".phpuser.com", 1); </span></span></li></ol>
Here are also There is one thing to note. For example, if your site has several different directories, if you only use cookies without a path, the cookies set on the pages in one directory will not be visible on the pages in another directory. , that is to say, Cookie is path-oriented. In fact, even if the path is not specified, the WEB server will automatically pass the current path to the browser, and specifying the path will force the server to use the set path. The way to solve this problem is to add the path and domain name when calling SetCookie. The format of the domain name can be "www.phpuser.com" or ".phpuser.com".
The part representing value in the SetCookie function will be automatically encoded when passed. That is to say, if the value of value is "test value", it will become "test%20value" when passed, which is the same as the URL. The method is the same. Of course, this is transparent to the program because PHP automatically decodes the cookie value when it receives it.
If you want to set multiple cookies with the same name, use an array. The method is:
<ol class="dp-xml"> <li class="alt"><span><span>SetCookie("CookieArray[]", "Value 1"); </span></span></li> <li class=""><span>SetCookie("CookieArray[]", "Value 2"); </span></li> </ol>
or
<ol class="dp-xml"> <li class="alt"><span><span>SetCookie("CookieArray[0]", "Value 1"); </span></span></li> <li class=""><span>SetCookie("CookieArray[1]", "Value 2"); </span></li> </ol>

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



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

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

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,

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

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.

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.
