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

藏色散人
Release: 2018-10-17 17:48:59
Original
4492 people have browsed it

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;];
Copy after login

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;];
Copy after login

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!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template