PHP session for beginners
1. What is session
The session variable is used to store information about the user session (session), or to change the settings of the user session (session). Session variables store information for a single user and are available to all pages in the application
2.php session variables
You operate a certain When you open an application, you open it, make changes, and then close it. It's a lot like a conversation. The computer knows who you are. It knows when you open and close apps. However, on the Internet a problem arises: since HTTP addresses cannot maintain state, the web server has no idea who you are and what you do.
PHP session solves this problem by storing user information on the server for subsequent use (such as user name, purchased items, etc.). However, session information is temporary and will be deleted after the user leaves the site. If you need to store information permanently, you can store the data in a database.
The working mechanism of Session is to create a unique id (UID) for each visitor and store variables based on this UID. The UID is stored in a cookie or passed through the URL.
3.Open session
session_start();
<?php session_start(); $_SESSION['name'] = "admin"; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>session</title> </head> <body> </body> </html>
As shown in the above code, the session has been opened. When we want to To store or retrieve data from the session, first we need to open the session
After opening the session, the data stored in the session can be called by other pages
In the session variable Store data
<?php session_start(); $_SESSION['name'] = "admin"; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>session</title> </head> <body> </body> </html>
In this way, we store an admin value in the variable $_SESSION['name']
Thenoutput session
<?php session_start(); $_SESSION['name'] = "admin"; ?> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>session</title> </head> <body> <?php echo "欢迎您:".$_SESSION['name']."!"; ?> </body> </html>
When we write in another file
<?php
echo "Welcome:".$_SESSION['name']."!";
?>
You can also output the value of name in the session
Destroy the session
If you want to delete some session data, you can use unset () or session_destroy() function.
unset() function is used to release the specified session variable
session_distroy() is generally used to destroy the session
For example, for two pages, the first page, we use session But it is not closed. When it reaches the second page
<?php
session_distroy();
?>
In this way the session Empty
The difference between session and cookie
1. Cookie data is stored in On the client's browser, the session data is placed on the server.
2. Cookies are not very safe. Others can analyze the COOKIE stored locally and conduct COOKIE deception
Considering security, session should be used.
3. The session will be saved on the server within a certain period of time. When access increases, it will take up more of your server's performance
Considering reducing server performance, COOKIE should be used.
4. The data saved by a single cookie cannot exceed 4K. Many browsers limit a site to save up to 20 cookies.
5. So personal suggestion:
Store important information such as login information as SESSION
If other information needs to be retained, it can be placed in COOKIE