Simple PHP session Description: session

Source: Internet
Author: User
Tags php session

Simple PHP session Description: session

Nowadays, programmers are getting harder and harder to master. To be proficient, it is necessary to trace the source. This is actually the opposite of the increasingly popular advanced languages and a large number of frameworks, because they try to cover up the source to make it simple, I personally call it a programmer's learning paradox.

Note: The author has been engaged in web development and php for about two weeks. The following content is suitable for beginners.

 

1. For more information, see <simple PHP Session description>. The scope of the content of this article has been defined, trying to clarify the Session) in PHP, how to implement and use PHP depends on the Usage After basic understanding. The idea of this article is to first understand what the session is, what the Session is used, what the routine is used by the Session, and how it is used in PHP. 2. Before the Session Description starts, we first recommend a basic theoretical book "HTTP authoritative guide", which is a basic knowledge for programmers. The author has an electronic version and can leave a message if necessary. A. The origins of sessions almost everyone accesses the Internet. Hundreds of millions of data are transmitted to each other over the network. The reason why data can be transmitted securely is based on the HTTP protocol. In fact, what the HTTP protocol does is to provide a series of methods to complete your network access. The two sides have constructed an access, which is in principle a session. For example, James entered the https://www.baidu.com/in the browser (HTTPS is the encrypted version of HTTP, compared to the added SSL encryption layer), which James initiated a request to Baidu, said: "I want to see your interface", Baidu's servers have received the information, which includes what the nickname wants, at the same time, it includes Xiao Ming's address (otherwise Baidu does not know who the content is to), the server checks the information OK, applies for the filing of Xiao Ming's request, and sends out what Xiao Ming wants, A complete request is complete. This is a session. The core of the session lies in James's information record filing (in fact, it also involves the TCP/IP connection problem. This is irrelevant to this article and is ignored ). In fact, instead of building a Session, it is better to generalize an access into a Session. B. what can I do with a Session? I can get it from the above content. Every access is a Session, and the server has to file information. This is costly. At the same time, it is unlikely that the same person will build and save ten times and 20 times for ten consecutive visits and 20 times. One is to increase the overhead, and the other is also stupid. That is to say, a person (exactly the same computer and browser) can reuse a Session at a specific time. Why? Because the Session has a default expiration time, the server will be cleared after the expiration time (if this is not the case, you think there are so many people in the world to keep one, it is too bad ). OK. Since the same person has multiple accesses to a Session (do not suspect that the server cannot identify the same person. For details, refer to the books introduced above ), in addition, the content accessed each time is for Record Filing. That is to say, all the behavior servers in your Session cycle are known, so the next important role will come, by analyzing your access requests, the server can learn the behavior preferences of this specific user. By doing some analysis, it can push some data that users like to care about, this is also the case for ad targeting. Of course, there may be other users, such as performance, etc. Individuals do not particularly understand the mechanism. This is the case here. 3. Using Session in PHP, we can find that the concept of Session actually occurs on the server side. PHP provides a series of configurations, functions, and so on, which can effectively implement the Session function. Session supports storing certain data in PHP by a method during concurrent access. this allows you to build more custom programs to improve the attractiveness of your web site. when a visitor accesses your website, a unique id is assigned, which is the session id. this id can be stored in a cookie on the user side or transmitted through a URL. session support allows you to save the data in the request to an ultra-Global Array $ _ SESSIONMedium. when a visitor accesses your website, PHP will automatically check (if session. auto_start is set to 1) or check (explicitly through session_start () or implicitly through session_register () whether the current session id is created in the previous request. in this case, the previously stored environment will be rebuilt. a. in php, session basic usage by assigning a unique Session ID to each independent user, you can implement the function of storing data for different users separately. Sessions are usually used to save and share information between multiple page requests. Generally, Session IDs are sent to the browser through cookies, and the server also uses Session IDs to retrieve Session data. If the request does not contain Session ID information, PHP creates a new Session and assigns a new ID to the newly created Session.

The workflow of the Session is very simple. When starting a Session, PHP will try to find the Session ID (usually through Session cookie) from the request. If the request does not contain the Session ID information, PHP will create a new Session. After the Session starts, PHP will set the data in the Session$ _ SESSIONVariable. When PHP stops, it automatically reads the content in $ _ SESSION, serializes it, and sends it to the SESSION Storage Manager for saving. By default, PHP uses the built-in file Session Storage Manager (files) To save the Session. You can also modify the session save Manager by configuring Session. save_handler (configuration item in php. ini. For the file Session Storage Manager, the Session data is saved to the location specified by the configuration item session. save_path (configuration item in php. ini. You can manually start a session by calling the session_start function. If session. auto_start is set1When the request starts, the Session starts automatically. After the PHP script is executed, the session is automatically closed. You can also manually close a session by calling the session_wirte_close () function.

In B. php, the session information is configured here in php. ini because it does not indicate the previous problem. The ghost knows what the configuration in php. ini is. The session. save_handler and session. save_path mentioned above are the configuration items in php. ini, which are not detailed here, because the php manual is too detailed. The default mode in this document is files. C. the session mechanism session_start () in php is the beginning of the session mechanism. The session determines whether $ _ COOKIE [session_name ()]; session_name () currently exists and returns the COOKIE key value for saving session_id, if it does not exist, a session_id is generated, and the generated session_id is passed to the client as the COOKIE value. the following COOKIE operation is performed. Conversely, if session_id =$ _ COOKIE [session_name] exists, then go to the session. find 'sess _ 'in the folder specified by save_path _'. session_id () file. read the file content deserialization and put it in $ _ SESSION. When the Session ends, the Session write operation or the session_write_close () operation is performed manually. There are three methods to destroy sessions in the Code: 1. setcookie (session_name (), session_id (), time ()-8000000,...); // execute before exiting the logon
2. usset ($ _ SESSION); // This will delete all $ _ SESSION data. After refreshing, a COOKIE is sent, but no data exists.
3. session_destroy (); // Delete $ _ SESSION Delete session file and session_id

Reference a piece of code on the network as an appendix.

 

<Span> // call function open ($ save_path, $ session_name) {global $ sess_save_path; $ sess_save_path = $ save_path; return (true);} during SESSION Initialization );} // call function close () {return (true);} function read ($ id) {global $ sess_save_path; $ sess_file = "$ sess_save_path/sess _ $ id"; return (string) @ file_get_contents ($ sess_file);} // before the script execution ends, execute the write operation function write ($ id, $ sess_data) {global $ sess_save_path; $ sess_file = "$ sess_save_path/sess _ $ id "; if ($ fp = @ fopen ($ sess_file, "w") {$ return = fwrite ($ fp, $ sess_data); fclose ($ fp); return $ return ;} else {return (false) ;}} function destroy ($ id) {global $ sess_save_path; $ sess_file = "$ sess_save_path/sess _ $ id "; return (@ unlink ($ sess_file);} function gc ($ maxlifetime) {global $ sess_save_path; foreach (glob ("$ sess_save_path/sess _ *") as $ filename) {if (filemtime ($ filename) + $ maxlifetime <time () {@ unlink ($ filename) ;}} return true ;}

 

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.