In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to complete the setting, acquisition and deletion of Session in PHP". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In the previous article, we introduced the cookie used by the client to store user data. In this article, we take a look at session,session, which plays a very important role in PHP, which is a server-side mechanism and is also used to store information. Compared with the cookie saved on the client side, the session users saved on the server side cannot be disabled. When the same client interacts with the server, it does not need to return all cookie values every time. All you need to do is return an ID, which is generated the first time you access the server, and is unique.
Then let's take a look at what session is and how to set, get and delete session.
What is session?
Session in Chinese means session, which is used to store the user's relevant data, which is similar to cookie, such as user name, personalization, etc., unlike cookie, cookie saves the data on the client's computer, which the user can disable, while session saves the data under the server system. The web page is a stateless program connection, which can not record the status of the user, so it is particularly important to record the relevant information of the user through session.
When a session is enabled, PHP randomly creates a sessionID, and each user's sessionID is unique. The sessionID will be saved on both the client and the server, in the specified directory where the client will use cookie, and in the form of recall text on the server to the specified session directory.
Compared with cookie, session has many advantages:
Because session data is not passed back and forth between the client and the server, session is generally more secure; session can store much more information than cookie; users can disable cookie, but there is a way for session to work properly.
Now that you know what session is, let's take a look at how to open session.
Turn on session
Unlike cookie, cookie can be created directly, but it must be started before using session, so that the core programs in PHP can preload the built-in environment related to session into memory.
You can enable session through the session_start () function in PHP. The syntax format of the function session_start () is as follows:
Session_start ([array $options = array ()])
Among them, it should be noted that:
Options is an optional function, is an associative array, and the keys in this array do not need to contain session. Prefix.
Examples are as follows:
Output result:
It is important to note that calling the session_start () function generates a unique Session ID and saves it in the browser's Cookie, with the default name "PHPSESSID". At the same time, generate a Session file composed of "sess_" and Session ID in the local directory, which is used to store the data in Session and output the result:
Now that you know how to turn on session through the above example, let's take a look at how to set and get session
Set and get session
In the above, after starting session, you also need to set and get the data in session if you want to use the session variable, which needs to be done through the $_ SESSION array. You must try the session_start () function to turn on session before using $_ SESSION.
$_ SESSION is an associative array, which has the same meaning as a normal associative array key-value pair. The syntax format for setting Session is as follows:
$_ SESSION [name] = value
Examples are as follows:
Output result:
After running, it should be noted that these variables or arrays will be saved to $_ SESSION, and will also be saved to the server-side file named by "sess_" and Session ID. The location of this file can be modified by modifying the php.ini configuration file or by using session.save_path configuration.
Now that you've seen how to open, set, and get session, let's take a look at how to delete session.
Delete a single session
To delete a single session element, you need to use the unset () function, which releases the specified variable, which is equivalent to directly canceling the element in the array. Its syntax format is as follows:
Unset (mixed $var [, mixed $...])
Among them, it should be noted that:
$var is the variable to be released, and the unset () function can accept multiple parameters, separated by parameters.
Examples are as follows:
Output result:
This completes the deletion of a single element of session through the unset () function.
Delete multiple elements of session
If you want to delete multiple Session elements at once, that is, log out all session variables at once, you can do so by assigning an empty array to $_ SESSION
Examples are as follows:
Output the result by assigning an empty array to $_ SESSION:
Another way is to release all the elements in the session through the session_unset () function, as an example:
The output is the same as that of the above example, so we can delete multiple elements of session in two ways.
This is the end of the content of "how to set, get and delete Session in PHP". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.