note 110352 added to function.session-set-save-handler

From: Date: Sun, 14 Oct 2012 19:52:02 +0000
Subject: note 110352 added to function.session-set-save-handler
Groups: php.notes 
Request: Send a blank email to php-notes+get-191289@lists.php.net to get a copy of this message
Be aware that reading and writing a session requires locking! The internal save handlers are supposed to do the locking, and at least the "files" save handler works in this respect. Unfortunately this documentation does not mention the fact that not locking can lead to destroyed session data. Please test the code at http://pastebin.com/EbqigxmY - it uses the code from example #2 on this page to show what not locking sessions can do. Expected behaviour is that all three iframes will be filled one after another with a 1 second break between every response. The last response should list all three requests and the master request. Actual behaviour is that all iframes are filled in parallel, and every single one dumps only the master request and itself. If you implement your own session save handler, never forget the locking! That's what the callbacks for "open" and "close" are for. You need an exclusive write lock from the start, otherwise a parallel request will be able to read stale data and overwrite the changes made. Yes, this costs performance, or more precisely this will serialize all requests that start the session. There are basically two methods to improve this: 1. Call session_write_close() as early as possible in every script. 2. You might implement two session handlers, one for reading and writing (including locking), and another one only for reading. If you never write back, there is no need for a lock, provided that at the time you read, no other write process interferes. ---- Server IP: 69.147.83.199 Probable Submitter: 178.26.58.146 ---- Manual Page -- http://www.php.net/manual/en/function.session-set-save-handler.php Edit -- https://master.php.net/note/edit/110352 Del: integrated -- https://master.php.net/note/delete/110352/integrated Del: useless -- https://master.php.net/note/delete/110352/useless Del: bad code -- https://master.php.net/note/delete/110352/bad+code Del: spam -- https://master.php.net/note/delete/110352/spam Del: non-english -- https://master.php.net/note/delete/110352/non-english Del: in docs -- https://master.php.net/note/delete/110352/in+docs Del: other reasons-- https://master.php.net/note/delete/110352 Reject -- https://master.php.net/note/reject/110352 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#191289) next »