Home > Article > Backend Development > What is Session_PHP Tutorial
First explain what a Session is, if you don't know. A Session starts when a visitor comes to your website and ends when he leaves. Essentially, cookies are related to the browser, and Session variables can store some resource variables on the server. PHP4 uses files to store Session variables, but in theory you can use a database or shared memory to do this. All pages use PHP4 Session and must use the Session_start() function to tell the PHP4 engine to retrieve the relevant Session into memory. The function Session_start() can obtain the Session_id in the cookie field or request parameters in order to respond to the http request. If the SessionID cannot be found, create a new Session. What are Session variables? The Session variable is a regular global variable. When a Session variable is registered, the Session value can be obtained on all pages using PHP4. Use Session_register("variable_name") to register a Session variable. Use the Session_start() function for all concurrent uses of Session, and the value of the variable will be registered as a Session variable. What can we do? There are usually many ways to manage Sessions and Session variables, I will give you an example. Say you are building a commercial site, like mine, you may want to keep the current names of users who have been acknowledged, or how many new messages the user has received. In order not to read from the database, you have two methods: 1.1. You can use three cookies. authenticated_user - the current user name. num_messages - the number of messages he got. expire_time - when to re-read the amount of information 2.2. Use sessions and create three new session variables. The first method has poor security. Some people can get cookies and enter other people's fields. With sessions, the user only gets one cookie, which is much safer. Disadvantages: Session gives you freedom. Excessive use of session will affect the use of scripting languages. Although PHP4's session has some limitations, for example, you cannot store objects in the session. Give it a try yourself!