PHP cookie method study notes sharing_PHP tutorial
Cookies in PHP are no different from those in other programs. Cookies are used to store information on the client. They are commonly used in some applications with low security requirements, such as users logging in and remembering passwords. Below I Let me introduce to you the php cookie study notes.
PHP setcookie() function sends an HTTP cookie to the client. A cookie is a variable sent to the browser by the server. 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.
The cookie must be assigned before any other output is sent. The function returns true if successful, false otherwise.
1 setcookie(name, value, expire, path, domain, secure)
•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.
The value of the cookie named "user" can be accessed via $HTTP_COOKIE_VARS["user"] or $_COOKIE["user"]. 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, php setting and getting cookies
The code is as follows | Copy code | ||||||||
//Function prototype: int setcookie(string name, string value, int expire, string path, string domain, int secure)
echo($mycookie);
|
Delete Cookies
代码如下 | 复制代码 |
setcookie('mycookie','',time()-3600); |
(2) Make the expiration time be time() or time-1;
The code is as follows | Copy code | ||||
setcookie('mycookie'); or setcookie('mycookie',''); or setcookie("mycookie",false);
|
The code is as follows | Copy code |
setcookie('mycookie','',time()-3600); |
The code is as follows | Copy code |
$y2k = mktime(0,0,0,1,1,2000); setcookie('name','value',$y2k); setcookie('name', 'value', time+3600); setcookie('name', 'value', $y2k, '~/myhome', '.domain.com'); |
How to get COOKIE expiration time
The code is as follows | Copy code | ||||
setcookie ("var_name", "var_value", $expire); // Set a cookie named var_name and set the validity period setcookie ("var_name_expire", $expire, $expire); // Set the expiration time into the cookie so that you can know the expiration time of var_name
|
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.
For example, cookie to save user login information
代码如下 | 复制代码 |
//数据库的位置 |
The code is as follows | Copy code |
//Database location<🎜> define('DB_HOST', 'localhost');<🎜> //Username<🎜> define('DB_USER', 'root');<🎜> //Password<🎜> define('DB_PASSWORD', '19900101');<🎜> //Database name<🎜> define('DB_NAME','test') ;<🎜> ?> |
2. Login page: logIn.php
The code is as follows | Copy code |
//Insert relevant information about connecting to the database $error_msg = ""; if(!empty($user_username)&&!empty($user_password)){ & Lt;!-Through the $ __Cookie ['user_id'], it is judged. If the user fails to log in, the login form is displayed so that the user enters the username and password-& gt; If(empty($_COOKIE['user_id'])){ echo ' '.$error_msg.' ';?> } ?> |
3、登入页面:loged.php
代码如下
|
复制代码
|
||||||||
//已登录页面,显示登录用户名
if(isset($_COOKIE['username'])){
'; //点击“Log Out”,则转到logOut.php页面进行cookie的注销 echo ' Log Out('.$_COOKIE['username'].')'; } /**In the logged in page, you can use the user's cookies such as $_COOKIE['username'], * $_COOKIE['user_id'] queries the database and can do many things*/ ?> 4、注销cookie页面:logOut.php(注销后重定向到lonIn.php)
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 UndressAI-powered app for creating realistic nude photos ![]() AI Clothes RemoverOnline AI tool for removing clothes from photos. ![]() Undress AI ToolUndress images for free ![]() Clothoff.ioAI clothes remover ![]() AI Hentai GeneratorGenerate AI Hentai for free. ![]() Hot Article
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
Assassin's Creed Shadows: Seashell Riddle Solution
2 weeks ago
By DDD
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months ago
By 尊渡假赌尊渡假赌尊渡假赌
![]() Hot Tools![]() Notepad++7.3.1Easy-to-use and free code editor ![]() SublimeText3 Chinese versionChinese version, very easy to use ![]() Zend Studio 13.0.1Powerful PHP integrated development environment ![]() Dreamweaver CS6Visual web development tools ![]() SublimeText3 Mac versionGod-level code editing software (SublimeText3) ![]() Hot Topics
CakePHP Tutorial
![]() ![]() ![]() 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. ![]() |