We participate in the Amazon Service LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com

PHP Sessions: Part 4

Session Locking. Some people don’t ever consider this. I have dealt with it before in another language, so I decided to do a quick search to see what it looks like in PHP.

The basic idea is that only one process can have a file open for writing at a time. Session information is kept in files, and since you can have multiple hits coming into your web page simultaneously, things have to be processed in order, in a queue. This can cause your site to slow down under heavy load.

There are techniques you can use to help speed things up.

What I found is this:

// start the session

// I can read/write to session
$_SESSION['latestRequestTime'] = time();

// close the session

// now do my long-running code.

// still able to read from session, but not write
$twitterId = $_SESSION['twitterId'];

// dang Twitter can be slow, good thing my other Ajax calls
// aren't waiting for this to complete
$twitterFeed = fetchTwitterFeed($twitterId);

echo json_encode($twitterFeed);

For an explanation of what’s going on here, check out the original post at  http://konrness.com/php5/how-to-prevent-blocking-php-requests/