PHP session blocking page Analysis and optimization (session_write_close session_commit use)

Source: Internet
Author: User
Tags close close php session

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).

    • Our problem
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.

    • Let's wrap it up.

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)

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.