- New in PHP 5.4 – Improvements in Session Extension
- Array Syntax and Dereferencing in PHP5.4
- Traits in PHP 5.4 – Introduction
- Traits in PHP 5.4 – Why we need Traits
- PHP 5.4 – built-in web-server, closures, callable and other new features
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.
[php]; 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
session.upload_progress.name = "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"[/php]
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 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
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.