Home>Article>Backend Development> PHP study notes: Application of Session and Cookie

PHP study notes: Application of Session and Cookie

王林
王林 Original
2023-10-08 08:34:23 764browse

PHP study notes: Application of Session and Cookie

PHP study notes: Application of Session and Cookie

In Web development, in order to record and maintain the user's status and data, it is often necessary to use Session and Cookie. Data storage and management. This article will introduce the concepts and principles of Session and Cookie and how to apply them in PHP.

1. Application of Session

Session is a mechanism used by the server to store user information. It saves user information on the server side, and each user will be assigned a unique Session ID to identify his or her Session. Through Session, we can share user data between different pages and provide personalized services.

1. Create Session

Before using Session, you need to enable the Session function first. In PHP, this can be achieved through the session_start() function:

session_start() function will create a unique Session ID on the server and send the ID to the client through Cookie.

2. Setting and getting Session data

Set Session data using the $_SESSION super global variable, and set the data by assigning a value to it:

Getting Session data only needs to pass $_SESSION variable to access:

3. Destroy Session

When the user exits or is inactive for a period of time, the Session generally needs to be destroyed to release server resources. You can use the session_destroy() function to destroy the Session and release all Session variables through the unset() function:

2. Cookie application

Cookie is a small browser-side stored Text data used to record user information. By setting cookies, we can obtain previously saved data the next time the user visits the website, and implement functions such as remembering login status.

1. Set Cookie

Use the setcookie() function to set Cookie:

The parameters of the setcookie() function have the following meanings:

  • The first parameter is the name of the cookie;
  • The second parameter is the value of the cookie;
  • The third parameter is the expiration time of the cookie. You can use the time() function to get the current time. And add a time interval on this basis. For example, time() 3600 means it will expire in 1 hour;
  • The fourth parameter is the path of the cookie. By default, cookies are only valid in the path where the script that sets it is located and its subpaths.

2. Read Cookie

You can use the $_COOKIE super global variable to read the value of Cookie:

It should be noted that using the $_COOKIE variable What is read is the cookie data in the last request, not the current request. Cookies set in the current request will only take effect on the next request.

3. Delete Cookie

To delete a cookie, you only need to set its expiration time to a past time:

By setting the expiration time to time() - 1. Cookies can be invalidated immediately to achieve the effect of deletion.

Conclusion

Through the introduction of this article, we have learned about the basic concepts, principles and applications of Session and Cookie in PHP. Using Session and Cookies, we can easily manage user status and data to achieve a more personalized and high-quality user experience. I hope this article will be helpful to everyone in the process of learning PHP.

The above is the detailed content of PHP study notes: Application of Session and Cookie. 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