Does PHP Close the File After the File Handler Is Garbage Collected

Does PHP close the file after the file handler is garbage collected?

PHP automatically runs the resource destructor as soon as all references to that resource are dropped.

As PHP has a reference-counting based garbage collection you can be fairly sure that this happens as early as possible, in your case as soon as $fh goes out of scope.

Before PHP 5.4 fclose didn't actually do anything if you tried to close a resource that had more than two references assigned to it.

should I close a file in PHP's __destruct() method?

I preffer to close the file when no more writing is need or in an exception:

try {
//write file
//close file after all reading operations has been done
} catch (Exception $e) {
fclose($this->handler);
}

This is good as an emergency or an uncatched problem, because PHP will close the file after deleting the object. If not, PHP interpreter will free all resources, and in Files it will free the memory address and any pending writing won't be done.

Does closing tab or leaving site call session __destruct in PHP?

The session is in PHP is not defined as a class. Instead, we have a set of session functions to manipulate the session. To make sure that you have destroyed the session, you need to explicitly call :

   session_destroy();

If you have not destroyed the session, the session will do garbage collection after the session timeout. The garbage collection depends on the following parameters - session.gc_maxlifetime, session.gc_divisor and session.gc_probability. To make sure that the garbage collection runs on every session, you will have to add session.gc_probability to 100%. But it definitely add an overhead to the server, especially if yours is a high traffic server.

If you do not explicitly keep track of the sessions and destroy it after use, you are leaving some part of the session management to the OS. See the note from PHP.net:

Note: If you are using the default file-based session handler, your
filesystem must keep track of access times (atime). Windows FAT does
not so you will have to come up with another way to handle garbage
collecting your session if you are stuck with a FAT filesystem or any
other filesystem where atime tracking is not available. Since PHP
4.2.3 it has used mtime (modified date) instead of atime. So, you won't have problems with filesystems where atime tracking is not
available.

The best way is to send a flag via Ajax call when the browser or tab is closed. You can detect it via : window.onunload event of javascript.

PHP Garbage Collector Keeps Removing My Sessions

According to your follow-up comments your setting for Session.handler is php. The Sessions documentation explains:

The built-in configurations are:

  • php - Saves sessions with the standard settings in your php.ini file.
  • cake - Saves sessions as files inside tmp/sessions. This is a good option when on hosts that don’t allow you to write outside your
    own home dir.

[…]

The default php.ini setting for session.save_path depends on your PHP distribution (and it can be changed anyway) but it normally involves a shared data storage for all PHP applications that do not opt out. That means that the app with the shortest session.gc_maxlifetime is likely to remove session data from other apps.

Switching to cake should address that.


A little follow-up about session.gc_probability and session.gc_divisor. Setting them too aggressively will cause frequent garbage collection. That may harm performance but it won't cause premature data expiration. On the other side, too loose values will still allow access to obsolete data.

cleanup php session files

To handle session properly, take a look at http://php.net/manual/en/session.configuration.php.

There you'll find these variables:

  • session.gc_probability
  • session.gc_divisor
  • session.gc_maxlifetime

These control the garbage collector (GC) probability of running with each page request.

You could set those with ini_set() at the beginning of your script or .htaccess file so you get certainty to some extent they will get deleted sometime.



Related Topics



Leave a reply



Submit