undefined
undefined

Reputation: 2101

Session blocked

I'm having trouble with using the PHP session.

I use AJAX to send a request to an action in an app, that is used just to trigger the starting of a process. The progress of this is stored in a session variable.
The problem is that I can't access this variable from another action until the first one is finished.
Something like this:

public function startWorkingAction() {  
$namespace = new Zend_Session_Namespace('progressOfWork');  
$namespace->totalItems = 0;   
$namespace->processedItems = 0;  
//... processing items  
$namespace->totalItems = $itemCount;   
foreach($items as $item) {  
//process a single item  
$namespace->processedItems++;  
}  
}

And I have another action to check the progress so far:

public function checkProgressAction() {  
$namespace = new Zend_Session_Namespace('progressOfWork');  
echo json_encode(array(  
'total' => $namespace->totalItems,  
'processed' => $namespace->processedItems  
));  
}

Both actions are triggered with AJAX requests. The problem is that I can't access the session namespace until the first action is done working.
Where am I going wrong?

Upvotes: 0

Views: 189

Answers (1)

Charles
Charles

Reputation: 51411

The problem is that I can't access the session namespace until the first action is done working. Where am I going wrong?

You aren't. This is the intended behavior of PHP sessions that use the default "files" handler.

Upon session_start, PHP will obtain an exclusive file lock on the session file stored on disk. The lock will be released only when the session data has been written to disk and the file closed. There is no way to disable this behavior short of writing your own custom session handler.

Upvotes: 1

Related Questions