What is session? I didn’t understand it at first. Non-professional dictionaries translate it as meeting and session period. Let’s make an inappropriate metaphor
(Although inappropriate, the meaning is the same), session is the relationship between you and the website. Session plays a very important role in WEB technology. Since the web page is a stateless connection program, you cannot know the user's browsing status. Therefore we must
The user's relevant information is recorded through the session so that the user can confirm when providing a request to the web server again in this capacity, for example
For example, we often require users to log in on some websites, but how do we know that the user has logged in? If there is no session, the login information cannot be retained, so why not ask the user to provide the username on every page? name and password.
Of course, session is not only used for user identity authentication, but may also be used for other aspects, which we will mention later. Session is explained in Chinese as session period. A session begins when the user enters the URL of a site and ends when he leaves the site. Session first appeared in the dynamic scripting language ActiveServerPages. Its function is so powerful that it cannot be explained clearly in one sentence.
When PHP was still in version 3.0, sessions were its eternal pain. Although PHP has the advantages of fast execution speed, flexible use, and powerful functions, many website developers have abandoned PHP because of session problems, at least my boss thinks so. At that time, there were many PHP free function libraries that provided solutions for implementing sessions on PHP3, but they all felt unauthentic. It's like the mobile phone you bought for thousands of dollars comes with a rough straw bag. Although the functions are the same, it always feels awkward. The emergence of php4 has given PHP a chance to make a comeback on the session issue. Although its session implementation is not ideal (mainly due to efficiency issues), it is implemented by itself after all, and it can be actually used. So what do we use session for? You've been talking for a long time. If I don't use it, wouldn't you be suspected of selling paper? OK, let's see what the session is used for: Anyone who has worked on a website has this experience. The variables on one page (in this chapter all refer to server-side variables, the same below) cannot be used on the next page. Although there are some ways to achieve this, such as using forms, urlstrings, etc., some are inconvenient for users. Even if the form is automatically submitted, the delay is enough to suffocate under today's network conditions, and this Both methods significantly increase the burden on programmers. If you are developing a large project, these additional burdens cannot be ignored. With session, it is easier to handle. Variables registered in session can be used as global variables. What, global variables? Great. In this way, you know what it is used for: the most important ones are used for user identity authentication, program status recording, and parameter transfer between pages.
After talking about its benefits for so long, you are already tempted, but don’t be happy yet. It also has shortcomings: it is a variable saved in a file (of course it is not efficient, although other methods can be used, but it is very troublesome) , the object cannot be saved. In contrast, the session in asp can save object variables and use memory variables to save session variables. But why do we still use PHP? Haha, why, you can read this chapter from the beginning of this book, I guess you should understand it. If you still don’t understand, Faint, just start from the beginning again, I guarantee that you will become PHP expert ^_^.
How is session implemented? Haha, you must think it is very profound, let me tell you its secret. If you only save variables, many readers will understand that this is very simple, but as we said before, the http protocol is a stateless connection. How do you know who the variable belongs to and who the variable belongs to? ?Achieved using cookies in session implementation. The cookie exists on the client, that is, the user's machine. It stores the user's session ID, which is the session number. When the user's browser requests the server, the session ID is also sent to the server, so that the server can identify who you are. This way the variable can be identified. In this way, it is not difficult for us to understand why the session sometimes fails. If you don't believe it, you can try: There is the "Internet Options" menu on the "Tools" menu of IE. After opening it, select "Security"->"Custom Level" and change the "Allow use of each conversation" in the security settings. "Cookies" is set to disabled, and then see if the session can be used. Now you understand! However, PHP4 can automatically check the cookie status on the linux/unix platform. When the cookies are not available, the session ID will be automatically attached to the url and passed. This is its only advantage over asp in terms of sessions.