search
HomeDaily ProgrammingPHP KnowledgeHow to record and delete variables in cookies in PHP? (Pictures + Videos)

This article mainly introduces you to the specific implementation methods of cookie recording variables and deleting variables in PHP.

First of all, everyone needs to knowWhat is PHP cookie? What does cookie mean?

Cookie Commonly used to identify users, they are small files left by the server on the user's computer. Whenever the same computer requests a page through the browser, it also sends the cookie. With PHP, you can create and retrieve cookie values.

Simply put, when a user connects to the server for the first time and successfully logs in, the next time he requests the server, he still does not know which user the current request is.

The emergence of cookie will solve this problem very well. When you log in to the server for the first time, some data (cookie) will be returned to the browser, and then the browser will save it locally.

When the user sends a request again, the cookie data stored in the last request will be automatically transmitted to the server, and the server will use the browser cookie Can determine who the current user is.

Below we will introduce to you the implementation method of PHP cookie storage variables and deletion based on simple code examples.

1. Record cookie

<?php
setcookie("username", "PHP中文网", time()+3600);
echo $_COOKIE[&#39;username&#39;];

Different from the session method of opening, here we can directly pass the setcookie functionTo set cookies, the parameters respectively represent the cookie name, cookie value and cookie expiration time.

Note:

setcookie() function Sends an HTTP cookie to the client.

$_COOKIE represents an array of variables passed to the current script through HTTP Cookies.

So for the basic knowledge of session, friends who need it can refer to [How to store and delete variables in session in PHP? ], everyone is welcome to learn and learn.

When we access through the browser for the first time, the result is as follows:

How to record and delete variables in cookies in PHP? (Pictures + Videos)

This is because the variable information needs to be stored in the first step .

When we refresh the browser again, the value of "username" in the echo output cookie is as shown below:

How to record and delete variables in cookies in PHP? (Pictures + Videos)

2. Delete cookies

<?php
// 设置cookie,需要给cookie一个生成时间,如果想删除cookie直接讲cookie的生成时间设置为负的即可
setcookie("username", "PHP中文网", time()-3600);
echo $_COOKIE[&#39;username&#39;];

Here we can directly set the cookie generation time to negative.

This article is aboutPHP cookie records and deletion variables The specific method introduction is easy to understand. I hope it will be helpful to friends in need!

If you want to know more about PHP, you can follow the PHP Chinese website PHP video tutorial, Welcome everyone to refer to and study!

The above is the detailed content of How to record and delete variables in cookies in PHP? (Pictures + Videos). For more information, please follow other related articles on the PHP Chinese website!

Statement
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

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.