Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does every access to $_SESSION immediately involves an i/o with the file system?

Tags:

php

session

Every time I access data in $_SESSION, Does it immediately update the session file on the disk, or just once when the process goes down? Or every n bytes of data change (flush)?

This question is not necessarily about the specific file session handler, but every handler. (Does every touch in session immediately invoke an I/O of any kind, beside the storing of a normal variable in memory).

like image 911
Itay Moav -Malimovka Avatar asked Mar 31 '09 04:03

Itay Moav -Malimovka


People also ask

How can I access session in PHP?

Start a PHP Session A session is started with the session_start() function. Session variables are set with the PHP global variable: $_SESSION.

Where are PHP sessions stored?

PHP Session Start By default, session data is stored in the server's /tmp directory in files that are named sess_ followed by a unique alphanumeric string (the session identifier). By itself, the session_start() function doesn't add much functionality to a web page.

How can we register the variables into a session in PHP?

We can create the session by writing session_start() and destroy the session by using session_destroy(). You can access the session variable by writing $_session[“name”]. Let us understand how the session works from the following examples. Example 1: In the following, you can create the session by entering the name.

What is session in PHP with example?

PHP session is used to store and pass information from one page to another temporarily (until user close the website). PHP session technique is widely used in shopping websites where we need to store and pass cart information e.g. username, product code, product name, product price etc from one page to another.


2 Answers

As Matt wrote, it writes at the end of script execution by default. You can read about it here in session_write_close()

Session data is usually stored after your script terminated without the need to call session_write_close(), but as session data is locked to prevent concurrent writes only one script may operate on a session at any time. When using framesets together with sessions you will experience the frames loading one by one due to this locking. You can reduce the time needed to load all the frames by ending the session as soon as all changes to session variables are done.

like image 198
OIS Avatar answered Oct 05 '22 13:10

OIS


It writes it and the end of the process on my setup. I made a new _ session_ write_method:

public function _session_write_method($id, $sess_data) {
    var_dump(file_put_contents('/var/www/public_html/testing.txt', serialize($sess_data)));
    return(true);
}

and then:

$_SESSION['foo'] = 'bar';
while(true)

I executed the script,waited a few seconds and then ran 'sudo kill' on the process id. It did not write the serialized data to the file. I ran it again without the infinite loop and I got: int(22) at the very bottom of the page and testing.txt was successfully written to and contained: s:14:"foo|s:3:"bar";";

like image 26
Matt Avatar answered Oct 05 '22 12:10

Matt