In PHP, P's default session processor is Session.save_handler = files (i.e. file). If the same client concurrently sends multiple requests (such as Ajax sending multiple requests at the same time), and the script executes longer, it can cause the session file to block and affect performance. Because PHP executes session_start () for each request, it obtains a file exclusive lock that will not be released until the request processing is complete. In this way, multiple requests can cause blocking at the same time. The solution is as follows:
(1) After modifying the session variable, immediately use Session_write_close () to save the session data and release the file lock.
Session_Start ();
$_session[' test ' = ' test ';
Session_write_close ();
//do Something
(2) using the Session_set_save_handler () function is to implement custom session processing.
Function open ($savePath, $sessionName) { echo ' open is called ';
return true; } Function close () { echo ' close is called ';
return true; } Function read ($sessionId) { echo ' read is called ';
return '; } Function write ($sessionId, $data) { echo ' write is
Called ';
return true;
} Function destroy ($sessionId) { echo ' destroy is called ';
return true; } FUNCTION GC ($lifetime) { echo ' gc is called ';
return true; } Session_set_save_handler ("open", "close", "read", "write", "destroy", "GC");
register_shutdown_function ( ' session_write_close ' );
Session_Start (); $_session[' foo '] = ' bar ';
Of course, after PHP 5.4.0, you can use it by implementing the Sessionhandlerinterface interface or inheriting the Sessionhandler class.
class mysessionhandler extends sessionhandler { public function __construct () { } public function open ($save _path, $session _id) { &NBSP} public function close () {   &NBSP} public function create_sid () {  } public function read ($id) {  } public function write ($id, $data) { } public function destroy ($id) {  }} $handler = new mYsessionhandler ();
The
//2nd parameter registers the function Session_write_close () as the register_shutdown_function () function.
Session_set_save_handler ($handler, true);
You can implement and encapsulate the above code, using MySQL or other memory databases to manage session data. It can also solve the session data sharing problem when using the cluster.
for the hidden dangers of this PHP, I believe the authorities will have a good solution for some time.