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!