Home > Backend Development > PHP Tutorial > How to do cookie manipulation in PHP?

How to do cookie manipulation in PHP?

WBOY
Release: 2023-05-22 10:00:01
Original
1496 people have browsed it

In web development, cookies are often used to record the user's status and track user behavior. Cookies are pieces of data stored on the user's browser that can be easily transferred between the browser and the server. In PHP, operating cookies is very simple. This article will introduce how to operate cookies in PHP through the following aspects.

  1. Set Cookie

The way to set Cookie in PHP is to use the setcookie() function. The setcookie() function has the following syntax:

setcookie(name, value, expire, path, domain, secure);

Among them, name is the name of the Cookie; value is the value of the Cookie; expire is the expiration time of the cookie; path is the available path of the cookie; domain is the scope of the cookie; secure determines whether to use cookies only in HTTPS connections. Here is a simple example:

setcookie("user_id", "123456", time() 3600);
?>

above In the code, a cookie named "user_id" is set with a value of "123456" and an expiration time of one hour.

  1. Read Cookie

The way to read Cookie in PHP is to use the $_COOKIE super global variable. This variable is an array that contains information about all cookies in the current page. The following is an example of reading a cookie:

$user_id = $_COOKIE['user_id'];
echo "The user ID is: ".$user_id;
?>

In the above code, the value of the cookie named "user_id" is obtained by accessing the $_COOKIE array.

  1. Modify Cookie

The method of modifying Cookie is similar to the method of setting Cookie, also using the setcookie() function. It should be noted that when modifying cookies, you need to set the same name, path, domain name and other information. The following is an example of modifying Cookie:

setcookie("user_id", "654321", time() 3600, "/", "example.com");
?>

In the above code, modify the value of the cookie named "user_id" to "654321" and set its scope to "example.com".

  1. Delete Cookie

The way to delete Cookie is to set the expiration time of Cookie to a past time. The following is an example of deleting cookies:

setcookie("user_id", '', time()-3600);
?>

at In the above code, making the expiration time of the cookie named "user_id" earlier than the current time is equivalent to deleting the cookie.

In short, operating cookies is very simple. It should be noted that when setting, reading, modifying and deleting cookies, the same name, path, domain name and other information must be set to ensure correct operation. Proper use of cookies can help developers better track and record user behavior, thereby improving the user experience of web applications.

The above is the detailed content of How to do cookie manipulation in PHP?. 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