<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Troubles with Asynchronous Ajax Requests and PHP Sessions</title>
	<atom:link href="http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/feed" rel="self" type="application/rss+xml" />
	<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions</link>
	<description>Cleaning up the web with Ajax</description>
	<lastBuildDate>Thu, 09 Feb 2012 06:55:33 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
	<item>
		<title>By: dhonde</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-284843</link>
		<dc:creator>dhonde</dc:creator>
		<pubDate>Fri, 30 Jul 2010 18:12:34 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-284843</guid>
		<description>If in case the session vars written in the ajax script and used in another page or the landing page, you should use: session_register(&#039;&#039;);
and then set the value.. such variables will be then globally available as soon as they are set. This definitely solves the issues from the thread: 
http://www.phpfreaks.com/forums/index.php/topic,250223.msg1173027.html#msg1173027</description>
		<content:encoded><![CDATA[<p>If in case the session vars written in the ajax script and used in another page or the landing page, you should use: session_register(&#8221;);<br />
and then set the value.. such variables will be then globally available as soon as they are set. This definitely solves the issues from the thread:<br />
<a href="http://www.phpfreaks.com/forums/index.php/topic,250223.msg1173027.html#msg1173027" rel="nofollow">http://www.phpfreaks.com/forums/index.php/topic,250223.msg1173027.html#msg1173027</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: arcodesign</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-278820</link>
		<dc:creator>arcodesign</dc:creator>
		<pubDate>Thu, 11 Feb 2010 17:10:01 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-278820</guid>
		<description>I found an archived version of Pedro&#039;s link here: http://www.streetdirectory.com/travel_guide/156917/programming/ajax_requests_and_php_sessions_solution.html

And really there are quite a few classes at phpclasses.org that might be helpful to you. See: http://www.phpclasses.org/search.html?words=mysql+session and http://www.phpclasses.org/browse/package/1518.html for the specific class Pedro mentions.</description>
		<content:encoded><![CDATA[<p>I found an archived version of Pedro&#8217;s link here: <a href="http://www.streetdirectory.com/travel_guide/156917/programming/ajax_requests_and_php_sessions_solution.html" rel="nofollow">http://www.streetdirectory.com/travel_guide/156917/programming/ajax_requests_and_php_sessions_solution.html</a></p>
<p>And really there are quite a few classes at phpclasses.org that might be helpful to you. See: <a href="http://www.phpclasses.org/search.html?words=mysql+session" rel="nofollow">http://www.phpclasses.org/search.html?words=mysql+session</a> and <a href="http://www.phpclasses.org/browse/package/1518.html" rel="nofollow">http://www.phpclasses.org/browse/package/1518.html</a> for the specific class Pedro mentions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: PaddyPatPat</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-277847</link>
		<dc:creator>PaddyPatPat</dc:creator>
		<pubDate>Thu, 14 Jan 2010 09:17:55 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-277847</guid>
		<description>Sadly Pedro&#039;s link no longer works. Any other suggestions for how to deal/code PHP sessions with Ajax requests?</description>
		<content:encoded><![CDATA[<p>Sadly Pedro&#8217;s link no longer works. Any other suggestions for how to deal/code PHP sessions with Ajax requests?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pedro</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-73219</link>
		<dc:creator>Pedro</dc:creator>
		<pubDate>Wed, 23 Aug 2006 01:04:58 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-73219</guid>
		<description>I&#039;ve quoted this thread and provided a solution for the matter at &lt;a href=&quot;http://blog.milpe.com&quot; title=&quot;Ajax Requests and PHP Sessions&quot; rel=&quot;nofollow&quot;&gt;Ajax Requests and PHP Sessions Solution&lt;/a&gt;. Hope it helps everybody.</description>
		<content:encoded><![CDATA[<p>I&#8217;ve quoted this thread and provided a solution for the matter at <a href="http://blog.milpe.com" title="Ajax Requests and PHP Sessions" rel="nofollow">Ajax Requests and PHP Sessions Solution</a>. Hope it helps everybody.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: lun</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-27651</link>
		<dc:creator>lun</dc:creator>
		<pubDate>Thu, 15 Jun 2006 10:26:56 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-27651</guid>
		<description>it is lun</description>
		<content:encoded><![CDATA[<p>it is lun</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: plenque</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-24967</link>
		<dc:creator>plenque</dc:creator>
		<pubDate>Mon, 12 Jun 2006 01:55:21 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-24967</guid>
		<description>just to clarify the situation here...
php, with it&#039;s standard session module (fs), locks the complete session on every request. 
just search in the file ext/session/mod_files.c for:
flock(data-&gt;fd, LOCK_EX);
but if you replace the session handler with session_set_save_handler, no locking takes place, so you have to write your own (this is pretty obvious, because if you deploy a distributed session design, there&#039;s no way php can make a network wide lock).
like it&#039;s been suggested, memcached&#039;s increment/decrement/add could be used to ensure the proper locking.....</description>
		<content:encoded><![CDATA[<p>just to clarify the situation here&#8230;<br />
php, with it&#8217;s standard session module (fs), locks the complete session on every request.<br />
just search in the file ext/session/mod_files.c for:<br />
flock(data-&gt;fd, LOCK_EX);<br />
but if you replace the session handler with session_set_save_handler, no locking takes place, so you have to write your own (this is pretty obvious, because if you deploy a distributed session design, there&#8217;s no way php can make a network wide lock).<br />
like it&#8217;s been suggested, memcached&#8217;s increment/decrement/add could be used to ensure the proper locking&#8230;..</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Michael Johnston</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-12632</link>
		<dc:creator>Michael Johnston</dc:creator>
		<pubDate>Mon, 22 May 2006 11:37:13 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-12632</guid>
		<description>Concurrency problems with sessions is not new to ajax apps. Anyone who has built web apps with frames where the frames share backend state will have experienced them before. Just saying &quot;make sure your app doesn&#039;t read &amp; write the same data in different calls&quot; doesn&#039;t make you look very smart; just demonstrates you probably have little real-world experience. One particular real world gotcha is that different browsers load framesets in different ways, and even with locking sessions it can be tricky to iron out all the concurrency issues.

One way to deal with locking sessions is to use db backed sessions and roll your own locking mechanism. You need to use transactions, and in order to handle the occasional high-latency or interrupted request, you&#039;re going to want to set up your locks to expire if another request has been waiting a while. To balance fast response with avoiding having high traffic situations result in an exponential growth of db access, you&#039;ll want to have  the sleep period between polls to see if the lock is available grow rapidly and geometrically/exponentially. It&#039;s also wise to have available read-only sessions that don&#039;t acquire a lock and only use locking when it is necessary, to minimize overhead. A request should be able to decide it needs a write log part way through it&#039;s execution and ask for it.

Some databases already have a locking mechanisms that makes building this kind of setup easier.</description>
		<content:encoded><![CDATA[<p>Concurrency problems with sessions is not new to ajax apps. Anyone who has built web apps with frames where the frames share backend state will have experienced them before. Just saying &#8220;make sure your app doesn&#8217;t read &amp; write the same data in different calls&#8221; doesn&#8217;t make you look very smart; just demonstrates you probably have little real-world experience. One particular real world gotcha is that different browsers load framesets in different ways, and even with locking sessions it can be tricky to iron out all the concurrency issues.</p>
<p>One way to deal with locking sessions is to use db backed sessions and roll your own locking mechanism. You need to use transactions, and in order to handle the occasional high-latency or interrupted request, you&#8217;re going to want to set up your locks to expire if another request has been waiting a while. To balance fast response with avoiding having high traffic situations result in an exponential growth of db access, you&#8217;ll want to have  the sleep period between polls to see if the lock is available grow rapidly and geometrically/exponentially. It&#8217;s also wise to have available read-only sessions that don&#8217;t acquire a lock and only use locking when it is necessary, to minimize overhead. A request should be able to decide it needs a write log part way through it&#8217;s execution and ask for it.</p>
<p>Some databases already have a locking mechanisms that makes building this kind of setup easier.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SitePoint Blogs &#187; AJAX and Session &#8220;Race Conditions&#8221;</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-4031</link>
		<dc:creator>SitePoint Blogs &#187; AJAX and Session &#8220;Race Conditions&#8221;</dc:creator>
		<pubDate>Mon, 27 Feb 2006 12:27:09 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-4031</guid>
		<description>[...] What&#8217;s troublesome is reading the type of responses to this problem, such as those in reply to AJAXian&#8217;s coverage of this. In particular the guys that should &#8220;get it&#8221; clearly don&#8217;t;  The thing about PHP is that it is request based, meaning that information storage is not persistent across requests. The session data is still saved/restored upon each request. If you cannot deal with this and need multiple asynchronous requests to a application to access the same dataset (why is beyond me, thereâ€™s other ways to code around that), then I suggest going to a J2EE platform. [...]</description>
		<content:encoded><![CDATA[<p>[...] What&#8217;s troublesome is reading the type of responses to this problem, such as those in reply to AJAXian&#8217;s coverage of this. In particular the guys that should &#8220;get it&#8221; clearly don&#8217;t;  The thing about PHP is that it is request based, meaning that information storage is not persistent across requests. The session data is still saved/restored upon each request. If you cannot deal with this and need multiple asynchronous requests to a application to access the same dataset (why is beyond me, thereâ€™s other ways to code around that), then I suggest going to a J2EE platform. [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-4003</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Sat, 25 Feb 2006 08:35:43 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-4003</guid>
		<description>&lt;blockquote&gt;@Andy, Keith, and Taylor: the article comments suggest that PHP already ensures only one thread has access to $_SESSION at a time.&lt;/blockquote&gt;
That comment is wrong.  If you overload the session handler, there is no locking function, which would be necessary in order to fully lock sessions when you have multiple web servers.  This would suggest that, while it might do locking if storing sessions in files, no locking can be done when you write your own session handler (like using mysql to store session data, for example).</description>
		<content:encoded><![CDATA[<blockquote><p>@Andy, Keith, and Taylor: the article comments suggest that PHP already ensures only one thread has access to $_SESSION at a time.</p></blockquote>
<p>That comment is wrong.  If you overload the session handler, there is no locking function, which would be necessary in order to fully lock sessions when you have multiple web servers.  This would suggest that, while it might do locking if storing sessions in files, no locking can be done when you write your own session handler (like using mysql to store session data, for example).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Gaughan</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3997</link>
		<dc:creator>Keith Gaughan</dc:creator>
		<pubDate>Fri, 24 Feb 2006 23:54:26 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3997</guid>
		<description>&lt;blockquote&gt;
The article comments suggest that PHP already ensures only one thread has access to $_SESSION at a time. The first thread that session_start()s gets exclusive access until it finishes or calls session_write_close(). While locked, other threads hault execution at the point they call session_start(). When the lock is released, execution continues in the next script. I canâ€™t find this in the PHP manual, but it should be trivial to verify.
&lt;/blockquote&gt;

Nope, it doesn&#039;t. Each request has a seperate &lt;em&gt;copy&lt;/em&gt; of the session, which is loaded on &lt;code&gt;session_start()&lt;/code&gt; and saved on &lt;code&gt;session_write_close()&lt;/code&gt;. No locking occurs; instead, the previous copy of the session is overwritten.

This behaviour is usually sufficient when you&#039;re dealing with people browsing a site. The time between two requests in the same session is almost always great enough that this race condition is never an issue. However, when you&#039;re dealing with a piece of code making simultaneous overlapping requests, the crap hits the fan.

It also has to be said that if PHP &lt;em&gt;did&lt;/em&gt; use the highly pessimistic locking scheme you described, the uses of sessions could cause it to run like treacle, with each request tied to a certain session being serialised. You&#039;d run into problems with memory consumption, tied up file handles, request timeouts, &amp;c. It&#039;s not a pleasant scenario.

&lt;blockquote&gt;
Iâ€™d think the easiest way to avoid the situation described is to add to your AJAX class the ability to keep track of requests that write to certain session vars. The client app can then decide if it should allow a 2nd write request to be sent before receiving the response from the 1st.
&lt;/blockquote&gt;

Safer might be to batch and/or serialise any such requests on the client. Wrap the XHR object in another object that maintains a queue of all pending requests and a list of any sent and pending completion (for callback). When the XHR object is free, send any pending requests together in one batch. It takes a little extra processing on each side to build and seperate each of the logical requests and responses, but it&#039;s worth it.</description>
		<content:encoded><![CDATA[<blockquote><p>
The article comments suggest that PHP already ensures only one thread has access to $_SESSION at a time. The first thread that session_start()s gets exclusive access until it finishes or calls session_write_close(). While locked, other threads hault execution at the point they call session_start(). When the lock is released, execution continues in the next script. I canâ€™t find this in the PHP manual, but it should be trivial to verify.
</p></blockquote>
<p>Nope, it doesn&#8217;t. Each request has a seperate <em>copy</em> of the session, which is loaded on <code>session_start()</code> and saved on <code>session_write_close()</code>. No locking occurs; instead, the previous copy of the session is overwritten.</p>
<p>This behaviour is usually sufficient when you&#8217;re dealing with people browsing a site. The time between two requests in the same session is almost always great enough that this race condition is never an issue. However, when you&#8217;re dealing with a piece of code making simultaneous overlapping requests, the crap hits the fan.</p>
<p>It also has to be said that if PHP <em>did</em> use the highly pessimistic locking scheme you described, the uses of sessions could cause it to run like treacle, with each request tied to a certain session being serialised. You&#8217;d run into problems with memory consumption, tied up file handles, request timeouts, &amp;c. It&#8217;s not a pleasant scenario.</p>
<blockquote><p>
Iâ€™d think the easiest way to avoid the situation described is to add to your AJAX class the ability to keep track of requests that write to certain session vars. The client app can then decide if it should allow a 2nd write request to be sent before receiving the response from the 1st.
</p></blockquote>
<p>Safer might be to batch and/or serialise any such requests on the client. Wrap the XHR object in another object that maintains a queue of all pending requests and a list of any sent and pending completion (for callback). When the XHR object is free, send any pending requests together in one batch. It takes a little extra processing on each side to build and seperate each of the logical requests and responses, but it&#8217;s worth it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: æ€?è€ƒã?¨ç¿’ä½œ</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3926</link>
		<dc:creator>æ€?è€ƒã?¨ç¿’ä½œ</dc:creator>
		<pubDate>Thu, 23 Feb 2006 17:41:07 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3926</guid>
		<description>&lt;strong&gt;Ajaxã?¨PHPã‚»ãƒƒã‚·ãƒ§ãƒ³ã?®ç«¶å?ˆ&lt;/strong&gt;

ãƒªãƒ³ã‚¯: Ajaxian ? Troubles with Asynchronous Ajax Requests and PHP Sessions  Ajaxianã?®ã‚µã‚¤ãƒˆã?«æŽ²è¼‰ã?•</description>
		<content:encoded><![CDATA[<p><strong>Ajaxã?¨PHPã‚»ãƒƒã‚·ãƒ§ãƒ³ã?®ç«¶å?ˆ</strong></p>
<p>ãƒªãƒ³ã‚¯: Ajaxian ? Troubles with Asynchronous Ajax Requests and PHP Sessions  Ajaxianã?®ã‚µã‚¤ãƒˆã?«æŽ²è¼‰ã?•</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: michael</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3921</link>
		<dc:creator>michael</dc:creator>
		<pubDate>Thu, 23 Feb 2006 15:33:51 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3921</guid>
		<description>Did you guys see another similar article (Java related)?
http://zk1.sourceforge.net/smalltalks/ajax-challenges/ajax-challenge1.html</description>
		<content:encoded><![CDATA[<p>Did you guys see another similar article (Java related)?<br />
<a href="http://zk1.sourceforge.net/smalltalks/ajax-challenges/ajax-challenge1.html" rel="nofollow">http://zk1.sourceforge.net/smalltalks/ajax-challenges/ajax-challenge1.html</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Stephen Clay</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3911</link>
		<dc:creator>Stephen Clay</dc:creator>
		<pubDate>Thu, 23 Feb 2006 13:22:55 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3911</guid>
		<description>@Andy, Keith, and Taylor: the article comments suggest that PHP already ensures only one thread has access to $_SESSION at a time. The first thread that session_start()s gets exclusive access until it finishes or calls session_write_close(). While locked, other threads hault execution at the point they call session_start(). When the lock is released, execution continues in the next script. I can&#039;t find this in the PHP manual, but it should be trivial to verify.

I&#039;d think the easiest way to avoid the situation described is to add to your AJAX class the ability to keep track of requests that write to certain session vars. The client app can then decide if it should allow a 2nd write request to be sent before receiving the response from the 1st.</description>
		<content:encoded><![CDATA[<p>@Andy, Keith, and Taylor: the article comments suggest that PHP already ensures only one thread has access to $_SESSION at a time. The first thread that session_start()s gets exclusive access until it finishes or calls session_write_close(). While locked, other threads hault execution at the point they call session_start(). When the lock is released, execution continues in the next script. I can&#8217;t find this in the PHP manual, but it should be trivial to verify.</p>
<p>I&#8217;d think the easiest way to avoid the situation described is to add to your AJAX class the ability to keep track of requests that write to certain session vars. The client app can then decide if it should allow a 2nd write request to be sent before receiving the response from the 1st.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3897</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Thu, 23 Feb 2006 05:21:47 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3897</guid>
		<description>It might be doable now without having to extend the Zend engine at all... in PHP5, after reading the session, assign _SESSION to be an object with __set and __get methods that perform the concurrency checks.  on write, convert the object to an array and write it.  PHP5 has some issues currently with storing objects in the session variable so there might be some issues to deal with that still.  You&#039;d have to access your session variables as $_SESSION-&gt;field rather than $_SESSION[&#039;field&#039;], so old code would have to be ported, but this is hardly a major hangup.</description>
		<content:encoded><![CDATA[<p>It might be doable now without having to extend the Zend engine at all&#8230; in PHP5, after reading the session, assign _SESSION to be an object with __set and __get methods that perform the concurrency checks.  on write, convert the object to an array and write it.  PHP5 has some issues currently with storing objects in the session variable so there might be some issues to deal with that still.  You&#8217;d have to access your session variables as $_SESSION-&gt;field rather than $_SESSION['field'], so old code would have to be ported, but this is hardly a major hangup.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Taylor</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3883</link>
		<dc:creator>Taylor</dc:creator>
		<pubDate>Wed, 22 Feb 2006 20:49:18 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3883</guid>
		<description>@Keith: Great response.  I totally agree with you.  The Zend engine can be extended.  A PHP extension can be written to replace the internal Session handling routines.  If you are interested, I&#039;d love to have a think tank session with you and see what the implications are.  Email me at tdondich@groundworkopensource.com.</description>
		<content:encoded><![CDATA[<p>@Keith: Great response.  I totally agree with you.  The Zend engine can be extended.  A PHP extension can be written to replace the internal Session handling routines.  If you are interested, I&#8217;d love to have a think tank session with you and see what the implications are.  Email me at <a href="mailto:tdondich@groundworkopensource.com">tdondich@groundworkopensource.com</a>.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Gaughan</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3881</link>
		<dc:creator>Keith Gaughan</dc:creator>
		<pubDate>Wed, 22 Feb 2006 19:59:42 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3881</guid>
		<description>And before I forget, to do that, PHP would also need some extra functionality, specifically a way to set handers for when values are read from or written to &lt;code&gt;$_SESSION.&lt;/code&gt;</description>
		<content:encoded><![CDATA[<p>And before I forget, to do that, PHP would also need some extra functionality, specifically a way to set handers for when values are read from or written to <code>$_SESSION.</code></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Gaughan</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3880</link>
		<dc:creator>Keith Gaughan</dc:creator>
		<pubDate>Wed, 22 Feb 2006 19:54:02 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3880</guid>
		<description>@Taylor: PHP uses that session persistence mechanisme because it&#039;s simple and portable, and works well with CGI. However there&#039;s absolutely no reason that the persistence mechanism couldn&#039;t use, say, mmap() to share session state across requests, or (god forbid) even a combination of SysV shared memory and semaphores, though I wouldn&#039;t recommend that.

The real problem is that PHP doesn&#039;t have any locking primitives.</description>
		<content:encoded><![CDATA[<p>@Taylor: PHP uses that session persistence mechanisme because it&#8217;s simple and portable, and works well with CGI. However there&#8217;s absolutely no reason that the persistence mechanism couldn&#8217;t use, say, mmap() to share session state across requests, or (god forbid) even a combination of SysV shared memory and semaphores, though I wouldn&#8217;t recommend that.</p>
<p>The real problem is that PHP doesn&#8217;t have any locking primitives.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Keith Gaughan</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3879</link>
		<dc:creator>Keith Gaughan</dc:creator>
		<pubDate>Wed, 22 Feb 2006 19:49:25 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3879</guid>
		<description>&lt;blockquote&gt;
Why in the hell would you want to write and read the same variable with different calls?
&lt;/blockquote&gt;

You do, each time you access any variables shared across a set of processes or threads. Few webservers process requests serially. PHP&#039;s lack of support for access control to shared datastructures (such as &lt;code&gt;$_SESSION&lt;/code&gt;) is a definite flaw in the langauge, cf. Java&#039;s &lt;code&gt;synchronized&lt;/code&gt; keyword or ColdFusion&#039;s &lt;code&gt;&lt;CFLOCK&gt;&lt;/code&gt; tag, both of which allow concurrent threads to access shared state without stepping on each other&#039;s toes.

You (and Tom, especially Tom for that ill-informed remark) should read up on concurrency. Web applications tend to be highly concurrent, and &lt;em&gt;nobody&lt;/em&gt; developing them should be without, at the very least, a solid grounding in concurrency.

And as Andy noted, this is a problem with most dynamic langauges at the moment, not just PHP. Very few provide proper support for taking read-only and excusive locks on shared state.</description>
		<content:encoded><![CDATA[<blockquote><p>
Why in the hell would you want to write and read the same variable with different calls?
</p></blockquote>
<p>You do, each time you access any variables shared across a set of processes or threads. Few webservers process requests serially. PHP&#8217;s lack of support for access control to shared datastructures (such as <code>$_SESSION</code>) is a definite flaw in the langauge, cf. Java&#8217;s <code>synchronized</code> keyword or ColdFusion&#8217;s <code>&lt;CFLOCK&gt;</code> tag, both of which allow concurrent threads to access shared state without stepping on each other&#8217;s toes.</p>
<p>You (and Tom, especially Tom for that ill-informed remark) should read up on concurrency. Web applications tend to be highly concurrent, and <em>nobody</em> developing them should be without, at the very least, a solid grounding in concurrency.</p>
<p>And as Andy noted, this is a problem with most dynamic langauges at the moment, not just PHP. Very few provide proper support for taking read-only and excusive locks on shared state.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gilles</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3871</link>
		<dc:creator>Gilles</dc:creator>
		<pubDate>Wed, 22 Feb 2006 17:22:59 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3871</guid>
		<description>I also use PHP and Sessions, and ajax. No problems at all.  My magic coins?
- initializing the session on the first page you come to
- session_write_close()
- DB backed sessions</description>
		<content:encoded><![CDATA[<p>I also use PHP and Sessions, and ajax. No problems at all.  My magic coins?<br />
- initializing the session on the first page you come to<br />
- session_write_close()<br />
- DB backed sessions</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andy</title>
		<link>http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions/comment-page-1#comment-3869</link>
		<dc:creator>Andy</dc:creator>
		<pubDate>Wed, 22 Feb 2006 16:58:43 +0000</pubDate>
		<guid isPermaLink="false">http://ajaxian.com/archives/troubles-with-asynchronous-ajax-requests-and-php-sessions#comment-3869</guid>
		<description>Concurrency issues have long since been solved, if you actually use the recommended methods of avoiding them.  Any modern book on threading will give you the basics.  With PHP it is especially problematic because each session &quot;variable&quot; is just an array index, and the entire session gets read or written at once, you can not access (or lock) just a single session &quot;variable&quot;.  But this is hardly a PHP only or AJAX only problem.  You would have the same kinds of issues with any series of multiple concurrent requests for the same page (like downloading images on the page that are dynamically generated based on session data).

The main issue here is that there are no built-in concurrency controls across multiple requests in popular server-side programming/scripting languages, so you&#039;d have to roll your own. Things like atomic increment in &lt;a href=&quot;http://www.danga.com/memcached/&quot; rel=&quot;nofollow&quot;&gt;memcached&lt;/a&gt; or &lt;a href=&quot;http://dev.mysql.com/doc/refman/4.1/en/miscellaneous-functions.html&quot; rel=&quot;nofollow&quot;&gt;get_lock/release_lock&lt;/a&gt; in mysql can help.  One may have to forgo using PHP&#039;s session handling to get finer grained locks, which means rolling your own session variable storage engine also.</description>
		<content:encoded><![CDATA[<p>Concurrency issues have long since been solved, if you actually use the recommended methods of avoiding them.  Any modern book on threading will give you the basics.  With PHP it is especially problematic because each session &#8220;variable&#8221; is just an array index, and the entire session gets read or written at once, you can not access (or lock) just a single session &#8220;variable&#8221;.  But this is hardly a PHP only or AJAX only problem.  You would have the same kinds of issues with any series of multiple concurrent requests for the same page (like downloading images on the page that are dynamically generated based on session data).</p>
<p>The main issue here is that there are no built-in concurrency controls across multiple requests in popular server-side programming/scripting languages, so you&#8217;d have to roll your own. Things like atomic increment in <a href="http://www.danga.com/memcached/" rel="nofollow">memcached</a> or <a href="http://dev.mysql.com/doc/refman/4.1/en/miscellaneous-functions.html" rel="nofollow">get_lock/release_lock</a> in mysql can help.  One may have to forgo using PHP&#8217;s session handling to get finer grained locks, which means rolling your own session variable storage engine also.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

