PHP Sessions - Locking and Sharing questions

后端 未结 2 1965
傲寒
傲寒 2021-01-13 14:53

I would like to know if it is possible to read $_SESSION attributes without locking it.
Currently, session_start() locks SESSION, that means other PHP proce

2条回答
  •  星月不相逢
    2021-01-13 15:25

    Personally I do this right at the start:

    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 the session variables.

    session_start();
    //This value will be "The #1 Value!" only the 2nd time you run this
    echo "
    myData value1:".$_SESSION['myData']; $_SESSION['myData'] = "Value 2 and 3!"; session_write_close(); echo "
    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 "
    myData value3:".$_SESSION['myData']; session_start(); //NOTE HOW THE ABOVE LINE WRITES-OVER $_SESSION echo "
    myData value4:".$_SESSION['myData']; $_SESSION['myData'] = "The #1 Value!"; session_write_close();

提交回复
热议问题