PHP Session optimization source code

Source: Internet
Author: User

PHP Session optimization source code

When a php session is requested by the same client, the server SESSION does not end and the next request cannot be performed!

For example, the following code:

Client:

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0"/>
<Meta name = "apple-mobile-web-app-capable" content = "yes"/>
<Meta name = "apple-mobile-web-app-status-bar-style" content = "black"/>
<Meta name = "format-detection" content = "telephone = no"/>
<Script type = "text/javascript" src = \ '# \' "/jquery-1.7.2.min.js"> </script>
<Style>
# Content {
Width: 100%;
Height: 100%;
Text-align: center;
}
</Style>
</Head>
<Body style = "margin: 0; padding: 0;">
<Script type = "text/javascript">
$ (Document). ready (function (){
For (var I = 1; I <= 5; I ++ ){
$. Ajax ({
Url: "rest. php? Call = "+ I,
Success: function (data ){
Var id = I;
$ ("Body"). append ("<p> Call complete" + data + "<p> ");
}
});
}
});
</Script>
</Body>
<Html>


Server:

<? Php
Session_start ();
Sleep (2 );
Echo '{json: "repsonse'. $ _ GET [" call "]. '"}';
?>

It takes more than 10 seconds to load the above client.

 

You can use session_write_close () to close the SESSION. The next request can be executed immediately!

Example:

<? Php

Session_start ();
Session_write_close ();
Sleep (2 );
Echo '{json: "repsonse'. $ _ GET [" call "]. '"}';
?>

The loading time of the above Code client is reduced from 10 s to 2 S!

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.