PHP was developed with the Session_Start () page enabled, because the execution time is too long, causing a user to be in a blocking state when accessing and another user is enabling Session_Start ().
The second can be enabled until the first user is finished. Start reading. This is the session blocking.
The session is saved as a file by default when a user accesses the Session_Start page. This time, a default is created that contains the session_id file name, and this time. The file is locked. If the user clicks on the link. Also visited a session file of the station, because the first page did not finish. He has been locking the file. So the second page can not acquire the lock, has been in a waiting state,. This way, if there is a large number of user access on the site, it will cause the session to read the file has been blocked waiting for the user's browser to keep the connection with the server to buy back consumes a lot of server resources, Web service station active connections will also increase. The connection resources may be consumed. A denial of server appears.
------Store user session with Memcache
Using Memcache to save the user session, compared to read the file has a great speed increase. And can do server sharing session is really convenient, this time will not appear with the file session lock clean, memcached read time, is shared, will not appear waiting, but we found. The number of memcached connections. Will still be maintained, and the number of connections will increase. If this time. The number of memcached connections you have set is too small. You will find. Soon the memcached died. This is also a long-appearing problem. , sometimes a lot of Web servers, session (Memcache) very few, found Memcache inexplicable dead. It might have something to do with it. Too many slow-reflecting pages (session enabled), the boulevard to occupy too many memcached connections.
Change session usage habits and optimize call methods
In fact, through the file or session, if the processing time-consuming pages, will bring the server resources are very expensive.
We generally write to the session or read the time, if we can control.
When you're done, shut down the file lock, or mem connection. will automatically release resources, in fact, PHP inside: session_write_close,session_commit function can be changed function.
<?PHPIni_set(' Session.save_path ', '/tmp/');functionOpen$save _path,$session _name){ Echo __function__, "<br/>"; return(true);}functionClose () {Echo __function__, "<br/>"; return(true);}functionRead$id){ Echo __function__, "<br/>";}functionWrite$id,$sess _data){ Echo __function__, "<br/>"; return(true);}functionDestroy$id){ Echo __function__, "<br/>"; return(true);}functiongc$maxlifetime){ Echo __function__, "<br/>"; return true;}Session_set_save_handler("Open", "close", "read", "write", "Destroy", "GC");register_shutdown_function(' Test ');functionTest () {Echo __function__, "<br/>";}Session_Start();Echo' AAAAA ', "<br/>";
Start "Session_Start" automatically executes, Open,read function, then the page executes, executes the shutdown function, finally writes the session into it, and then executes close close the file. From Session_Start to the end of the page, the file is locked or kept connected.
If we execute the Session_Start, execute "session_commit ();" Look at the results.
execution procedure: after executing commit, call directly, wirte,close operation. Close the file directly or close the connection (memcache).
1. How many times do we write the page?
After the first write, then commit, open the write again, and then commit again. We found that 2 times the data was saved to the user session.
1. Only read the session page, it is recommended to open, the direct commit, this is the $_session variable has been generated.
2. The session is written to the page, it is recommended to modify the $_session, directly call commit
3. Open and write multiple times, this is not recommended, compare open files, write is time-consuming. If you can fix it at once, don't do it many times. Unless, in the middle, a time-consuming business is performed.
PHP session blocking page Analysis and optimization (session_write_close session_commit use)