Analysis of PHP Session may cause concurrency problems _ php skills

Source: Internet
Author: User
Tags php session
It is estimated that many programmers will not think about the php session concurrency problem, because we generally do not use the session for concurrent operations, but sometimes it may also be used, today, we will briefly talk about this problem. when developing Web applications, people often use sessions to store data. However, some people may not know that improper use of sessions in PHP may cause concurrency problems. Kishan Gor, senior engineer of Plus91 Technologies, India's medical industry software solution provider, explained the problem on his personal blog.

If the same client sends multiple requests concurrently, and each request uses a Session, the existence of the PHP Session lock will cause the server to respond to these requests serially, rather than in parallel. This is because by default, PHP uses a file to store Session data. For each new Session, PHP creates a file and continuously writes data to it. Therefore, each time you call the session_start () method, the Session file is opened and the exclusive lock of the file is obtained. In this way, if the server script is processing a request and the client sends another request that needs to use Session, the next request will be blocked, until the previous request is processed, the exclusive lock on the file is released. However, this is only limited to multiple requests from the same client. that is to say, requests from one client do not block requests from the other client.

If the script is short, this is usually no problem. However, if the script runs for a long time, problems may occur. In modern Web application development, a very common situation is that AJAX technology is used to send multiple requests to obtain data on the same page. If all these requests require Session, the first request will get the Session lock after arriving at the server, and other requests will have to wait, and all requests will be processed in series, even if they are not dependent on each other. This greatly increases the page response time.

One way to avoid this problem is to immediately call session_write_close () to close the Session after the Session is used. In this way, the Session lock will be released even if the current script is still waiting for processing. It should be noted that after this method is called, the current script cannot further operate on the Session.

It is worth noting that the problems and opinions stated in this article only apply to the default Session management mode of PHP using session_start. For example, some users point out that if they host their applications on AWS EC2 and correctly configure DynamoDB, the Session lock issue will not occur.

Appendix example code:

Session. php

<? Phpfinal class SessionController extends YafController_Abstract {public function setUserFileAction () {session_start (); $ _ SESSION ['User _ name'] = 'xudianyang '; $ _ SESSION ['User _ id'] = '000000'; sleep (3); echo json_encode ($ _ SESSION); return false;} public function setLoginFileAction () {session_start (); $ _ SESSION ['last _ time'] = time (); echo json_encode ($ _ SESSION); return false;} public function indexFileAction () {// Auto Rend View} public function getSessionFileAction () {session_start (); var_dump ($ _ SESSION); return false;} public function setUserRedisAction () {$ session = CoreFactory :: session (); $ session-> set ('User _ name', 'xudianyang '); $ session-> set ('User _ id', '123 '); sleep (3); echo json_encode ($ _ SESSION); return false;} public function setLoginRedisAction () {$ session = CoreFactory: session (); $ session-> set ('last _ time', time (); echo json_encode ($ _ SESSION); return false;} public function indexRedisAction () {// Auto Rend View} public function getSessionRedisAction () {$ session = CoreFactory: session (); var_dump ($ _ SESSION); return false ;}} indexfile. phtml Test session concurrency lock 
  

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.