Home>Article>Daily Programming> How to record and delete variables in cookies in PHP? (Pictures + Videos)

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

藏色散人
藏色散人 Original
2018-10-17 17:48:59 4430browse

This article mainly introduces you to the specific implementation methods ofcookie recording variables and deleting variablesin PHP.

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

CookieCommonly 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 ofcookiewill 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, thecookie datastored in the last request will be automatically transmitted to the server, and the server will use the browsercookieCan determine who the current user is.

Below we will introduce to you the implementation method ofPHP cookie storage variables and deletionbased on simple code examples.

1. Record cookie


      

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

Note:

setcookie() functionSends an HTTP cookie to the client.

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

So for the basic knowledge ofsession, 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


      

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

This article is aboutPHP cookie records and deletion variablesThe 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 websitePHP 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