New in PHP 5.4 – Improvements in Session Extension

This entry is part 1 of 5 in the series New in PHP 5.4

The PHP 5.4 RC6 was released on 24th Jan 2012. The next release is expected on 2nd Feb 2012. The version is not final and thus, not recommended for production use. However, there are many interesting improvements that are already a part of the RC6 version. PHP 5.4 includes the best parts of the PHP 6. PHP 6.0 is now shelved with Zeev Suraski, CTO Zend Technologies, saying that it was too ambitious.

PHP 6 was supposed to be a complete rewrite of the language using Unicode instead of ASCII. Some of its improvements will be rolled out in the coming version. I guess that we will reach PHP 6 in small steps!!! There are many good additions to the session extension.

Object Oriented Session Handlers

PHP is fast moving from procedural language to a complete object oriented language. The new internal SessionHandler class allows the users to extend sessions handlers in object oriented manner. The SessionHandler class has 6 methods for initializing, reading, writing, closing, destroying and cleaning up the old sessions. Now, the SessionHandler class accesses the original session handler, which can be files, memcached, etc. The session_set_save_handler() also accepts an object of this class. You can transparently extend whatever handler is currently in use. At the same time, you can also call the parent implementation. I am playing with this and we will have another go at object oriented session handlers with an example.

File Upload Progress Feedback in Session Data

For some requirement you might have used the PECL uploadprogress package to get the progress of an uploaded file. PHP 5.4 allows you to get the progress of uploaded files through the session data. To use the feature, you have to configure the feature in the php.ini file. Listed below are the configuration parameters that are required to be enabled and set properly.

; Enable upload progress tracking in $_SESSION

session.upload_progress.enabled = On

; Cleanup the progress information as soon as all POST data has been read

session.upload_progress.cleanup = On

; A prefix used for the upload progress key in $_SESSION

session.upload_progress.prefix = "upload_progress_"

; The index name (concatenated with the prefix) in $_SESSION

; containing the upload progress information = "PHP_SESSION_UPLOAD_PROGRESS"

; How frequently the upload progress should be updated.

; Given either in percentages (per-file), or in bytes

session.upload_progress.freq =  "1%"

; The minimum delay between updates, in seconds

session.upload_progress.min_freq = "1"

Upload form is the standard html file upload form with an additional hidden field. This field defines the progress name attribute so that the upload can be identified in the session variables. If you name this hidden field as “upload”, then the progress can be accessed by prefixing the session.upload_progress.prefix value  to the hidden field.

So, on the submitted page, the upload progress can be accessed by using $_SESSION["upload_progress_upload"].

Session Status Variable

PHP had no function to test whether session was started or not. PHP 5.4 introduces a new function to fetch the current session status. The function is session_status().

echo session_status();


echo session_status();

//outputs 12

To make the function effective, the session.auto_start should be set to 0 in php.ini. Now, if no session exists, the session_status() will return 1 else if an active session is found then it will return 2. However, if the auto_start is set to 1 in php.ini, then, session_status() will always return 2. These return values are pre-defined constants for session extension.

Series NavigationTraits in PHP 5.4 – Introduction >>
  • Pingback: Array Syntax and Dereferencing in PHP5.4 | Refulz PHP

  • Hirvine Kinneas

    “PHP had no function to test whether session was started or not. ” … Isn’t session_id() not reliable ? I use it to determine if the session is actually started … it worked so far pretty good.

    • refulz

      Yes, session_id() is reliable, you may also use session_name(). The session_id() returns the ID of the current session and session_name() returns the name of current session.
      There was no specific function to actual check whether session exists or not. The session_check() will return PHP_SESSION_NONE, if no session exists, PHP_SESSION_ACTIVE if the session exists and PHP_SESSION_DISABLED if the session is disabled.
      However, you may continue to use session_id() or session_name() to check whether session exists or not.
      PHP 5.4 just has got this new additional function specifically for checking the existence of session. Obviously, before PHP 5.4, people were using various methods to reliably detect the presence of a session.
      Thanks for writing.

  • Pingback: PHP 5.4 – built-in web-server, closures, callable and other new features | Refulz PHP