Inner Mechanics of PHP Sessions

From the manuals, I draw that when php session var is installed, it is written to a text file in the session_save_path folder.

I'm just curious to know if this will happen as soon as the interpreter hits the line with the session variable or does it (write to a text file) when the PHP interpreter finishes processing the file?

For example, if I were to set and update a session variable in two consecutive lines (as in the example below), does the PHP interpreter save the files twice back?

In other words, which code snippets have the correct comment?

$_SESSION['my_variable']=1; // writes to the session text file $_SESSION['my_variable']=2; // writes to the session text file again die(); // versus $_SESSION['my_variable']=1; // updates the session file contents in the memory $_SESSION['my_variable']=2; // updates the session file contents in the memory die(); // writes to the session text file 
+4
source share
4 answers

Data is written to a file if:

  • session_write_close () is called
  • script completion completed

So, if you do not fulfill 1), your second assumption is true.

Writing to a file every time a variable changes will be VERY expensive because, generally speaking, access to the disk and writing to the disk is slow, so PHP will not do this. It should be noted, however, that caching systems such as memcache or redis will store changes as they occur, so it might be a good idea to rely on them when PHP sessions are lacking in terms of reliability.

+4
source

Second, $_SESSION[...] = ... they just set the value inside your $_SESSION , and die() runs this function:

session_write_close

(PHP 4> = 4.0.4, PHP 5)

session_write_close - Record session and end session data

Report an error Description

void session_write_close (void) End the current session and save the session data.

Session data is usually saved after the script completes without having to call session_write_close () , but since the session data is locked to prevent simultaneous writing, only one script can run on the session at any time. When using framesets along with sessions, because of this blocking, you will observe the loading of frames one by one. You can reduce the time required to download all frames by ending the session as soon as all changes to the session variables are completed.

http://nl.php.net/manual/en/function.session-write-close.php

+2
source

So, I tried to verify this by doing the following.

test_1.php

 session_start(); $_SESSION['my_variable']=1; sleep(20); exit; 

and

test_2.php

 session_start(); var_dump($_SESSION); 

Here are the cases I tested:

  • Run test_1.php , then quickly run test_2.php .

Result: test_2.php freeze until test_1.php finishes sleeping.

  • Executing test_1.php , removing the cookie PHPSESSID and THEN executing test_2.php .

Result: test_2.php runs immediately without freezing ( test_1.php was still asleep) and printed an empty array.



Beginner Output:

PHP opens the stream to a text file associated with this session until the script completes execution and then "makes" the changes. (I am not an expert, so my conditions may not be accurate)

Edit: another useless test due to lack of documentation consultation

but since the session data is locked, to prevent only one script from being written at a time, it can work in the session at any time

+2
source

Sessions will be recorded in cookies or in any other storage that you choose only after clearing the settings page. ex:

having pageA and pageB:

on page A:

session ["foo"] = "bar";

session ["foo"] will be available for the page only after :

leave page A on page

or
leaving the page and refreshing the page

which is considered the best, the definition of IMHO is second.

Happy coding!

-1
source

All Articles