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:23873browse

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:

<?php
session_start(); // 开启Session
?>

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:

<?php
$_SESSION['username'] = 'john'; // 设置Session数据
$_SESSION['age'] = 18; // 设置Session数据
?>

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

<?php
echo $_SESSION['username']; // 获取Session数据
echo $_SESSION['age']; // 获取Session数据
?>

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:

<?php
session_destroy(); // 销毁Session
unset($_SESSION); // 释放Session变量
?>

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:

<?php
setcookie('username', 'john', time() + 3600); // 设置Cookie,有效期为1小时
setcookie('age', 18, time() + 3600, '/'); // 设置Cookie,有效期为1小时,适用于整个网站
?>

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:

<?php
echo $_COOKIE['username']; // 获取Cookie的值
echo $_COOKIE['age']; // 获取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:

<?php
setcookie('username', '', time() - 1); // 删除Cookie
?>

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