PHP Sessions - Blocking and Sharing Issues

I would like to know if $ _SESSION attributes can be read without locking it.
session_start() is currently blocking SESSION, which means that other PHP processes will wait until they are unlocked.
But some processes just want to get some $ _SESSION variables, rather than writing to them.
Is it possible to implement some function like session_get(string $id) that does not block SESSION?

In addition, session_id('shared_vars_of_'.$userid) can be shared between browsers as soon as the user is logged into the same account, for example, using session_id('shared_vars_of_'.$userid) . But is it safe? Is this discouraging?

Thanks,
Oh well

+4
source share
2 answers

Interest Ask!

session_write_close () is not what you are asking for, but it should speed up the process:

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

A script that only needs read-only access can start a session, copy the session variables to another array and apply session_write_close (). This will not be a fully readable solution - you may need to create your own session handler for it, but this should be a big step forward.

Update: I just found an interesting problem since 2001 in the PHP 4 tracker , which seems to be a patch that allows read-only sessions - it doesn't seem to be in official releases, although at least not in accordance with the documentation! Maybe it's worth it to dig further or reopen a ticket for PHP 5.

+4
source

Personally, I do this at the very beginning:

 session_start(); session_write_close(); 

And then you have access to $ _SESSION as read-only. As you can see below, you do not need to copy session variables.

 session_start(); //This value will be "The #1 Value!" only the 2nd time you run this echo "<br />myData value1:".$_SESSION['myData']; $_SESSION['myData'] = "Value 2 and 3!"; session_write_close(); echo "<br />myData value2 (read-only):".$_SESSION['myData']; $_SESSION['myData'] = "Value 3 Misleading, and never actually written to the session!"; //But it will affect this value, obviously echo "<br />myData value3:".$_SESSION['myData']; session_start(); //NOTE HOW THE ABOVE LINE WRITES-OVER $_SESSION echo "<br />myData value4:".$_SESSION['myData']; $_SESSION['myData'] = "The #1 Value!"; session_write_close(); 
+6
source

Source: https://habr.com/ru/post/1311316/


All Articles