phpcodeigniterxamppphp-7.4

Multiple php requests simultaneously, second request doesn't finishe until first finishes


When I am using XAMP Server with PHP 5.6

When I am using XAMP Server with PHP 7.4.9

-- Anything I need to change XAMP configuration for request will not wait until first finish


Solution

  • To understand the issue and solution related to PHP session handling, let's clarify the scenario:

    When session_start() is called, PHP locks the session file to prevent concurrent writes and inconsistencies. This means if you open two scripts that both call session_start() and the first script takes a long time to execute, the second script will have to wait until the first script finishes.

    File1.php

      <?php
        session_start();
        sleep(1);
        echo "I am File1";
        ?>
    

    File2.php:

    <?php
    session_start();
    sleep(10);
    echo "I am File2";
    ?>
    

    When you run File2.php first, and then File1.php, File1.php will have to wait for File2.php to finish because the session file is locked by File2.php.

    Solution: To solve this issue, you can call session_write_close() after starting the session and before the long-running process. This releases the session file lock, allowing other scripts to start their session.

    File1.php:

    <?php
    session_start();
    session_write_close();
    sleep(1);
    echo "I am File1";
    ?>
    

    File2.php:

    <?php
    session_start();
    session_write_close();
    sleep(10);
    echo "I am File2";
    ?>
    

    Now, when you run File2.php first and then File1.php, File1.php will not wait for File2.php to finish because File2.php releases the session file lock immediately after starting the session.