通过多次ajax调用更新会话

发布于 2024-12-14 20:28:18 字数 482 浏览 1 评论 0原文

“问题:”
我使用 ajax 多次调用 handler.php 文件。

在 handler.php 中,我有:

session_start();
$_SESSION['foo'] .= 'abc';

echo 'Session var: '.$_SESSION['foo'].'<br>';

现在,我看到的是:

Session var: abc
Session var: abc
Session var: abc
... etc

而不是:

Session var: abc
Session var: abcabc
Session var: abcabcabc

问题是什么?
我希望你明白这一点:)

编辑:我忘了提到有时我会得到第二个版本(通常期望的),但大多数时候我会得到第一个版本。

"The Problem:"
I call with ajax a handler.php file multiple times.

In the handler.php I have:

session_start();
$_SESSION['foo'] .= 'abc';

echo 'Session var: '.$_SESSION['foo'].'<br>';

Now, what I see is:

Session var: abc
Session var: abc
Session var: abc
... etc

Instead of:

Session var: abc
Session var: abcabc
Session var: abcabcabc

Whats the problem?
I hope you get the point:)

EDIT: I forgot to mention that sometimes I get the second (what normally expect), but most of the time I get the first version.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

笑梦风尘 2024-12-21 20:28:18

也许在向其附加内容之前将 $_SESSION['foo'] 定义为空字符串:

session_start();

// prevent caching
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");

if (!isset($_SESSION['foo']))
  $_SESSION['foo'] = '';

$_SESSION['foo'] .= 'abc';

echo 'Session var: '.$_SESSION['foo'].'<br>';

同时检查您的浏览器是否启用了 Cookie。

Maybe define $_SESSION['foo'] as an empty string before appending something to it:

session_start();

// prevent caching
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Cache-Control: no-cache");
header("Pragma: no-cache");

if (!isset($_SESSION['foo']))
  $_SESSION['foo'] = '';

$_SESSION['foo'] .= 'abc';

echo 'Session var: '.$_SESSION['foo'].'<br>';

Check as well that Cookies are enabled on your browser.

赤濁 2024-12-21 20:28:18

我找到了解决方案......所以4个月后我可以回答它:)

解决方案看起来很简单,但我没有首先考虑它。我认为这是一个“更深层次”的问题,因为我的代码很长。所以解决方案是包含 session_start();在“index.php”文件中(您调用ajax本身或包含.js文件的位置)。
我希望如果将来有人遇到这些愚蠢的“傻瓜”,这会有所帮助。*
因此,即使您不在“index.php”文件中使用会话,如果您想在 ajax“处理程序”php 文件中使用会话,也必须在其中包含 session_start() 。

(当然,您也必须将 session_start(); 包含到 hander.php 中。)

I found the solution... so after 4 months I can answer it :)

The solution seems very easy but I did not think about it first. I thought it was a "deeper" problem, because my code is very long.. So the solution was to include session_start(); in the "index.php" file, (where you call the ajax itself or where you included the .js file).
I hope it helps if someone in the future run into these silly "simptoms".*
So, even if you don't use sessions in the "index.php" file, you have to include session_start() there if you wanna use sessions in the ajax "handler" php file.

(You have to include session_start(); to hander.php too, of course.)

断爱 2024-12-21 20:28:18

我知道这是一个非常古老的问题,但在遇到同样的行为后,我在确认今天早些时候读到的内容时偶然发现了这个问题。

对于我的应用程序,我们已经在使用 session_start() 并使用自定义数据库处理程序处理会话。但在各种 AJAX 请求中将数据记录到会话时,我们仍然遇到错误。

运行处理会话数据的并发 AJAX 请求的问题在于,PHP 默认情况下一次只允许一个脚本访问会话:从调用 session_start() 到退出或调用 session_write_close() 的时间。当您同时发生多个需要写入会话的 AJAX 请求时,它们可能会相互干扰,最终导致数据不完整。

这比我能更好地解释它:[ http://konrness.com /php5/how-to-prevent-blocking-php-requests]。

我的特定解决方案是将数据存储在会话之外(数据库中的其他位置),以便其他 AJAX 请求不会影响我的更改。

I know this is a super-old question, but I happened upon it while looking to confirm what I had read earlier today, after running into the same behavior.

For my application, we are already using session_start() and are handling the session with custom database handlers. But we were still getting errors when recording data to the session in various AJAX requests.

The problem with running concurrent AJAX requests that manipulate session data is that PHP by default only allows one script to have access to the session at a time: from the time it calls session_start() to the time it exits or otherwise calls session_write_close(). When you have multiple AJAX requests happening simultaneously that need to write to the session, they can step on each other and you end up with incomplete data.

This explains it better than I can: [ http://konrness.com/php5/how-to-prevent-blocking-php-requests ].

My particular solution was to store the data outside of session (elsewhere in the database) so that the other AJAX requests wouldn't step on my changes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文