A brief analysis of _php skills in PHP session may cause concurrent problems

Source: Internet
Author: User

In the development of Web applications, people often use the session to store data. But some people may not know that in PHP, improper session usage may cause concurrency problems. Indian Medical Industry software solutions provider PLUS91 Technologies Senior engineer Kishan Gor on the personal blog to explain this issue.

If the same client sends multiple requests concurrently, and each request uses a session, the presence of the PHP sessions lock causes the server to respond to these requests serially, rather than in parallel. This is because, by default, PHP uses files to store session data. For each new session,php, a file is created and the data is continuously written to it. Therefore, each time the Session_Start () method is invoked, the session file is opened and the file's exclusive lock is obtained. Thus, if the server script is processing a request and the client sends a request that also requires the session to be used, the latter request blocks until the previous request processing completes freeing the exclusive lock on the file. However, this is limited to multiple requests from the same client, i.e., requests from one client do not block requests from another client.

If the script is short, this is usually no problem. However, if the script is running for a long time, it may cause problems. In modern Web application development, a very common scenario is to use AJAX technology to send multiple requests to fetch data on the same page. If these requests need to use session, then the first request arrives at the server and gets the sessions lock, the other requests must wait, and all requests are serially processed, even if they are not dependent on each other. This will greatly increase the response time of the page.

One way to avoid this problem is to call the Session_write_close () method to close the session immediately after using the session. This will release the session lock even if the current script is still waiting to be processed. Note that when this method is invoked, the current script cannot further manipulate the session.

In particular, the issues and views stated in this article apply only to the PHP default session management mode using the Session_Start () method. For example, some users point out that if the application is hosted on the AWS EC2 and the dynamodb,session lock problem is properly configured, it will not occur.

Attach an example code:

session.php

<?php Final class Sessioncontroller extends Yafcontroller_abstract {public Function setuserfileaction () {SE
    Ssion_start ();
    $_session[' user_name '] = ' xudianyang ';

    $_session[' user_id '] = ' 123 ';
    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 () {ses
    Sion_start ();

    Var_dump ($_session);
  return false;
    The Public Function setuserredisaction () {$session = Corefactory::session ();
    $session->set (' user_name ', ' Xudianyang ');

    $session->set (' user_id ', ' 123 ');
    Sleep (3);
    echo Json_encode ($_session);
  return false;
    The Public Function setloginredisaction () {$session = Corefactory::session ();

    $session->set (' Last_time ', Time ()); ECho Json_encode ($_session);
  return false; The Public Function indexredisaction () {//Auto rend View} public Function Getsessionredisaction () {$
    Session = Corefactory::session ();

    Var_dump ($_session);
  return false; }} indexfile.phtml <!
 DOCTYPE html>  

The above is the entire contents of this article, I hope you can enjoy.

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.