如何保护PHP中的关键部分?

发布于 2024-08-27 20:32:32 字数 203 浏览 7 评论 0原文

我对这个主题做了一些搜索,但没有发现任何有价值的东西。

如果我不使用 PHP 默认会话处理程序,则请求级别没有会话锁定。所以,我必须自己保护关键部分。

在 Java 中,我们有同步。在 C# 中,我们有

在 PHP 中,如何做到这一点?

I did some search about this topic but found nothing valuable.

If I don't use PHP default session handler, there is no session lock at request level. So, I have to protect critical section by myself.

In Java, we have synchronized. In C#, we have lock.

In PHP, how to do that?

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

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

发布评论

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

评论(3

温馨耳语 2024-09-03 20:32:32

为此,请使用信号量

Use semaphores for that.

沧笙踏歌 2024-09-03 20:32:32

由于 PHP 脚本不是多线程的,我不认为关键部分是相关的。我同意您有竞争条件,因为多个实例并行运行,但关键部分无法解决您的问题。

如果您使用基于文件的会话处理程序,则可以尝试基本的 flock()。如果使用数据库,您可以尝试使用引擎自己的(最好是行级)锁定机制。如果使用memcached,可以尝试实现分布式锁系统。

您不会想太广泛地应用锁。您需要尽可能精细的信息(也许与会话 ID 相关)。如果您尝试序列化所有会话行为,则会引入巨大的瓶颈。

在数据库世界(和其他地方),乐观“锁”通常就是您所需要的。它们涉及一个递增的简单计数器。如果计数为“关闭”(冲突),则记录不会更新,您可以根据需要重新获取并应用差异。这是一个与重试相结合的 UPDATE 表 WHERE count = lastcount 类型的东西。这通常能达到目的。

当然,您可以使用文件锁定、SYSV 信号量(少量)和各种其他方法来实现此目标。请记住,这听起来像是竞争条件,但它与线程无关。

Since PHP scripts aren't multi-threaded, I don't believe critical-sections are relevant. I agree that you have a race condition because multiple instances are run in parallel, but a critical section won't solve your problem.

If you're using a file-based session handler, you can try a basic flock(). If using a database, you can try using the engine's own (preferably, row-level) locking mechanism. If using memcached, you can try implementing a distributed lock system.

You won't want to apply a lock too broadly. You'll want something as granular as possible (tied to the session ID, perhaps). If you attempt to serialize all session behavior, you'll introduce a massive bottleneck.

In the database world (and elsewhere), optimistic "locks" are often all you need. They involve a simple counter that is incremented. If the count is "off," (collision) the record isn't updated and you can re-fetch and apply your differences as appropriate. It's an UPDATE table WHERE count = lastcount type thing combined with retry. This often does the trick.

Of course, you can use file locking, SYSV semaphores (sparingly) and a variety of other methods to achieve this goal. Just remember, this sounds like a race condition, but it has nothing to do with threads.

清晰传感 2024-09-03 20:32:32

大多数时候你不必处理临界区,因为 PHP 没有线程模型(不共享内存)。我什至不认为您可以使用semephores。 PHP 使用“不共享架构”,如您在PHP 创建者 Rasmus Lerdorf 的幻灯片。这意味着正如您在幻灯片上看到的那样

共享数据被下推到
数据存储层

只需在 apache 之上运行一个简单的 PHP 脚本,您甚至不必处理关键部分。您必须通过数据库与数据进行交互。
您还可以使用文件并锁定它们,但我会使用数据库而不是文件。

希望这能稍微回答您的问题。

Most of the times you don't have to deal with critical section, because PHP does not have a thread-model(no sharing of memory). I don't even think semephores is available to you. PHP uses a "Share-nothing Architecture" as you can see on of the slides of PHP creator Rasmus Lerdorf. This means as you can see on the slide that

Shared data is pushed down to the
data-store layer

With just a simple PHP script running on top of apache you don't even have to deal with critical sections. you have to interact with data via your database.
You could also use files and lock them, but I would go with database instead of files.

Hope this answers your question a little bit.

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