Maintaining Sessions and Authentication in HTML5/Flash Uploader PHP

Supported technologies: Adobe Flash

If you are going to utilize Flash Uploader in non-Internet Explorer browsers (Firefox, Safari, and etc.), you should take care about keeping your session and authentication cookies saved if they are used. The fact is that Flash Uploader does not send cookies in output requests in non-Internet Explorer browsers by default and, therefore, session and authentication tickets are lost during the upload. To work around this problem you can employ special facilities provided either by HTML5/Flash Uploader PHP or HTML5/Flash Uploader JavaScript. Let us consider how to use them.

Using HTML5/Flash Uploader PHP

If you use HTML5/Flash Uploader PHP, session and authentication information will be automatically passed to the upload request. If you are unfamiliar with HTML5/Flash Uploader PHP, please, see the Quick Start with HTML5/Flash Uploader PHP topic.

To restore session server-side you need to use restoreSession(), like follows:

PHP
require_once 'ImageUploaderFlashPHP/UploaderUtils.class.php';
if (UploaderUtils::restoreSession()) {
    //Save files
}
else {
    //Handle the error
}

Using HTML5/Flash Uploader JavaScript API

If you prefer not to use HTML5/Flash Uploader PHP, then you should add cookies to the upload request manually via the HTML5/Flash Uploader JavaScript. To do this, use the metadata.addCustomField(String, String, Boolean) method which accepts cookie in the following format: "cookieName=cookieValue". The most convenient place to call this method is the BeforeUpload event handler, because this event fires when the upload is about to be started.

JavaScript
function beforeUploadHandler(){
    var fu = $au.imageUploaderFlash('Uploader1');
    fu.metadata().addCustomField('PHPSESSID','<?=$_COOKIE["PHPSESSID"]?>',true);
};

var fu = $au.imageUploaderFlash({
    id: 'Uploader1',
    events: {beforeUpload: beforeUploadHandler},
});
fu.writeHtml();

To restore session server-side you need to use restoreSession(), like follows:

PHP
require_once 'ImageUploaderFlashPHP/UploaderUtils.class.php';
if (UploaderUtils::restoreSession()) {
    //Save files
}
else {
    //Handle the error
}

See Also

Reference

Manual