Session Full Course (i)

Source: Internet
Author: User
session| Tutorial One, session overview

What is the session, just at the beginning I do not understand, the non-professional dictionary translation for the meeting, the meeting period. Make an inappropriate analogy.
(although not appropriate, but the meaning is the same), the session is the relationship between you and the site. Session plays a very important part in Web technology. Because a Web page is a stateless connection, you cannot know the user's browsing status. So we must
Record the user's information through the session so that the user can confirm the request to the Web server again in this capacity, for example
For example, we often require users to login in some sites, but how do we know that the user has logged in, if there is no session, the login information can not be retained, it is not to let users in every page of the page to provide user name and password.


Of course, the session is not only for the user identity authentication function, but also may be used in other aspects, we will mention later. Session in Chinese to explain is conversational period. A session period begins when the user enters a site's URL, and ends when he leaves the site. The session, which first appeared in the Dynamic scripting language active Server pages, is a powerful feature that cannot be clearly explained.


When PHP is still in the 3.0 version, session is its eternal pain. Although PHP has the implementation speed, the use of flexible, powerful and so on, but because of the session of the problem, so many site development gave up PHP, at least my boss thinks so. At that time, there are many PHP free function library to provide a session on the PHP3 program, but all make people feel not authentic. It's like you spend thousands of of dollars to buy a mobile phone but configure a very rough grass as a bag, although the function is the same, but always make people feel awkward. The advent of PHP4 has given PHP a chance to roll over on session issues. Although its session implementation is not very ideal (mainly the problem of efficiency), but it is realized by itself, and can be used in practice. Then we use the session to do, you said half a day, I do not use, you will not sell paper suspicion. OK, let's see what's the use of the session: People who have done the site have this experience, in a page of variables (in this chapter, the server-side variables, the same below) can not be used in the next page, although there are some ways to achieve, such as using form,urlstring and so on, But some are inconvenient for the user, even if the form is submitted automatically, but the delay in the current network conditions enough to suffocate, and both of these methods significantly increase the burden on the programmer. If you are developing a large project, these additional burdens cannot be overlooked. And with the session is good to do, the session of the registered variable can be used as a global variable. What, global variables? Great. In this way, you know what is the use of it: the most important for user identity authentication, program status records, the parameters of the transfer between pages.


said it so half-day benefits, you have been tempted to, do not happy, it has shortcomings: it is a file to save the variable (of course, not high efficiency, although can be used in other ways, but very troublesome), can not save objects. In contrast, the session in ASP can save the object variable and use the memory variable to save the session variable. But why we also choose PHP, Oh, why, you can see this chapter from the beginning of this book, you should also understand it, you do not understand, faint, you look from the beginning, I guarantee you to become a PHP expert ^_^.


How is the session implemented? Oh, you must think very advanced, I come to tell you its secret. If you only save variables, many readers understand that this is very simple, but as we said earlier, the HTTP protocol is a stateless connection, how do you know who the variable is, and whose is this variable? implemented in the session implementation with cookies. Cookies exist in the client, that is, the user's machine, which holds the user's session ID, which is the session number, when the user's browser request server, the session ID also sent to the server, so that the server can identify who you are, You can also identify the variable. So it's not hard to understand why the session sometimes fails. If you don't believe it, you can try it: on IE, the Tools menu has the Internet Options menu, open and then "security"-> "Custom Level", the security settings in the "Allow the use of each dialog cookies" set to disable, and then see if the session can be used. That's clear! However, PHP4 on the Linux/unix platform can automatically check the status of cookies, when the cookies are not available, the session ID will automatically be shipped with the URL to pass. This is the only advantage it has over ASP in terms of session.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.