Should I call a variable that contains the SESSION value instead of calling the actual session in PHP?

If the PHP session variable is stored in a file (for example, by default), then let's say that I save the username in the session variable ...

$_SESSION['username'] = 'Jason Davis'; 

Now that the page is built, if I call $ _SESSION ['username'] 100 times during the page build process, will it succeed with the session files and read them 100 times?

Same thing, but with a session stored in MySQL. Will he query the database 100 times to get the username from the session table?

I'm just trying to figure out if I should name the session variable 1 time per page, and then store it in a local variable and use it for others 99 times. Like this...

 $username = $_SESSION['username']; echo $username; // 100 times throughout all the files that build my page. 

Note. Please understand that this is just an example, in fact I will need to access a simpler username session and 100 times, most likely will be less, but will be distributed across several keywords and session values

+4
source share
2 answers

No, session data is read when session_start is called and written when script or session_write_close runs out of time.

+3
source

The file is read only when the session_start () function is called.

As for MySQL, if you make a request, get the username and save it in a variable, there will be no further requests. If you store something in a variable, then copy it and should not do something with the original value from which you got it.

Regards, Llamas

0
source

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


All Articles