Home  >  Article  >  Backend Development  >  Detailed explanation of the function of getting started with PHP session 1_PHP tutorial

Detailed explanation of the function of getting started with PHP session 1_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:40:02671browse

What is session? I didn’t understand it at first. Non-professional dictionaries translate it as meeting, meeting period. To use 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 record the relevant information of the user through the session so that the user can confirm when he again provides a request to the web server in this capacity. For example, we often require users to log in on some websites, but how do we know that the user has already logged in? After logging in, if there is no session, the login information cannot be retained. Then why not ask the user to provide a user name and password on every web page.

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, session was its eternal pain. Although PHP has the advantages of fast execution speed, flexible use, and powerful functions, many site developers have abandoned PHP because of the session problem, 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 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.

Having talked about its benefits for so long, you are already tempted. Don’t be happy yet. It also has shortcomings: it is a variable saved in a file (of course it is not efficient, although it can be used in other ways , but 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 choose 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 status of cookies on the Linux/Unix platform. When 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/486241.htmlTechArticleWhat is session? I didn’t understand it at first. Non-professional dictionaries translate it to meeting, meeting period. Let’s make an inappropriate metaphor (although it is inappropriate, the meaning is the same), session is...
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