{"componentChunkName":"component---src-templates-blog-post-js","path":"/blog/2008/08/20/php-session-management-grievance-2/","result":{"data":{"markdownRemark":{"html":"<p>Sometimes PHP surprises you with an easy-to-use feature, like sessions.</p>\n<p>Sessions are quite easy to use in PHP. One call to <code>@session_start()</code>, and you have a magic global called <code>$_SESSION</code> to store data in; associated with the user using a cookie called <code>PHPSESSID</code>. PHP takes care of reading and writing the session data for you, and you think no more about it.</p>\n<p>Simple.</p>\n<p>Time passes, and you haven’t given sessions another thought. Your site’s evolving, using more and more AJAX, and seems to be performing ‘OK’. But, there’s a niggling doubt that something’s not quite right.</p>\n<p>For us, we realized something was wrong when we opened multiple search-results in separate windows. We could see the tabs were loading one by one, slowly.</p>\n<p>I guess we should have paid more attention to start with. Our previous web development background revolved around enterprise-class application servers. Sessions just worked, no concurrency worries. If you happened to run into a race-condition, you worked around it using threading and locking facilities provided by the implementation language. It never occurred to us that PHP would be so different.</p>\n<p>PHP, the way we’re running it (via <code>mod_php</code>) couldn’t be further from the application-server model if it tried. Sessions are implemented using file-based storage by default, not held in shared memory ready for use by multiple threads.</p>\n<p>Storing sessions in files means PHP has to take heavy-handed precautions against concurrent read/write access to the session - it locks the session file for the duration of a request.</p>\n<p>The idea never occurred to us - that session management would block user-requests, stopping concurrent requests completing (think AJAX.) Fortunately the quick-fix solution is simple: call <a href=\"https://www.php.net/session_write_close\"><code>session_write_close()</code></a> as soon as you’ve finished writing to the session. Depending how you use sessions, you may find a number of actions only need read-access to the session, in which case you may want to open and close the session together: <code>@session_start(); session_write_close()</code></p>\n<p>That’s the quick fix, but there are plenty of other options to explore to. A quick code-audit could identify a ton of actions, controllers and pages that simply don’t need session access at all. Now you know PHP locks the session file, you probably want to avoid calling <code>session_start()</code> unless absolutely necessary.</p>\n<p>Secondly, PHP allows you to choose what type of session-management you use. You can use memcached either on its own, or with a database backing-store. You could use a MySQL back-end, or roll your own session management registered using <a href=\"https://www.php.net/manual/en/function.session-set-save-handler.php\"><code>session_set_save_handler</code></a>. It’s really up to you.</p>\n<p>Perhaps that’s the problem right there. All the session-management hooks are there because the default session management sucks. The simplicity of using sessions lulls you into a false sense of security, but make no mistake - sessions need to be handled with care if you’ve any hope of running a high-volume website.</p>\n<p>Are your sessions managed properly?</p>\n<style class=\"grvsc-styles\">\n  .grvsc-container {\n    overflow: auto;\n    position: relative;\n    -webkit-overflow-scrolling: touch;\n    padding-top: 1rem;\n    padding-top: var(--grvsc-padding-top, var(--grvsc-padding-v, 1rem));\n    padding-bottom: 1rem;\n    padding-bottom: var(--grvsc-padding-bottom, var(--grvsc-padding-v, 1rem));\n    border-radius: 8px;\n    border-radius: var(--grvsc-border-radius, 8px);\n    font-feature-settings: normal;\n    line-height: 1.4;\n  }\n  \n  .grvsc-code {\n    display: table;\n  }\n  \n  .grvsc-line {\n    display: table-row;\n    box-sizing: border-box;\n    width: 100%;\n    position: relative;\n  }\n  \n  .grvsc-line > * {\n    position: relative;\n  }\n  \n  .grvsc-gutter-pad {\n    display: table-cell;\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  .grvsc-gutter {\n    display: table-cell;\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter::before {\n    content: attr(data-content);\n  }\n  \n  .grvsc-source {\n    display: table-cell;\n    padding-left: 1.5rem;\n    padding-left: var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem));\n    padding-right: 1.5rem;\n    padding-right: var(--grvsc-padding-right, var(--grvsc-padding-h, 1.5rem));\n  }\n  \n  .grvsc-source:empty::after {\n    content: ' ';\n    -webkit-user-select: none;\n    -moz-user-select: none;\n    user-select: none;\n  }\n  \n  .grvsc-gutter + .grvsc-source {\n    padding-left: 0.75rem;\n    padding-left: calc(var(--grvsc-padding-left, var(--grvsc-padding-h, 1.5rem)) / 2);\n  }\n  \n  /* Line transformer styles */\n  \n  .grvsc-has-line-highlighting > .grvsc-code > .grvsc-line::before {\n    content: ' ';\n    position: absolute;\n    width: 100%;\n  }\n  \n  .grvsc-line-diff-add::before {\n    background-color: var(--grvsc-line-diff-add-background-color, rgba(0, 255, 60, 0.2));\n  }\n  \n  .grvsc-line-diff-del::before {\n    background-color: var(--grvsc-line-diff-del-background-color, rgba(255, 0, 20, 0.2));\n  }\n  \n  .grvsc-line-number {\n    padding: 0 2px;\n    text-align: right;\n    opacity: 0.7;\n  }\n  \n</style>","timeToRead":2,"frontmatter":{"title":"PHP Session Management (grievance 2)","blurb":"Spawning multiple requests to a PHP-backed can trigger blocking behaviour that requires a shift in how you manage server-side session state","date":"2008-08-20T10:15:20.000Z","modified":"2011-01-03T17:20:14.000Z"}}},"pageContext":{"slug":"/blog/2008/08/20/php-session-management-grievance-2/","next":{"fields":{"slug":"/blog/2008/08/22/getting-git-to-work-on-os-x-tiger/"},"frontmatter":{"title":"Getting git to work on OS X Tiger"}},"previous":{"fields":{"slug":"/blog/2008/08/08/svn-log-message-encoding-problem/"},"frontmatter":{"title":"SVN log message encoding problem"}}}},"staticQueryHashes":["1192980692"]}