PHP 中是否有一个单例类链接到会话?
我有一个单例类,我将其用作验证码系统的一部分来生成代码和图像等。
我在 html 表单中包含一个脚本,用于加载单例类,生成代码和图像,并将其输出到浏览器。然后我有另一个脚本来验证和处理表单。这会加载类单例以检索之前创建的实例,并调用该函数来验证代码。
我遇到的问题是,当我验证表单时,表单上生成的代码已更改或在我验证表单时完全不存在!
我还没有在 php 会话中启动或存储任何内容,但是在加载表单的页面上创建了一个会话。单例的实例是否以某种方式链接到该会话?如果它是一个命名会话或其他什么?
或者...我完全误解了单例类的工作原理吗?在这种情况下,谁能告诉我如何检索在 html 表单页面上创建的类的实例,以便再次使用来验证表单处理脚本中的代码? - 也许告诉我应该如何使用单例!
非常感谢。
I have a singleton class that I am using as part of a CAPTCHA system to generate the code and image etc.
I have one script included on the html form that loads the class singleton, generates the code and image, and outputs it to the browser. I then have another script to validate and process the form. This loads the class singleton to retrieve the instance that was created previously, and calls the function to validate the code.
The problem I'm having is that when I'm validating the form the code that was generated on the form has changed or is completely absent when I come to validate it!
I haven't started or stored anything in the php session, but a session is created on the page the form is loaded in. Is the instance of the singleton somehow linked to that session? If it's a named session or something?
OR...have I completely misunderstood how singleton classes work? In which case can anyone tell me how I can retrieve the instance of the class that is created on the html form page to use again to validate the code in the form processing script? - And maybe tell me how I should be using singletons!
Many thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
单例在请求期间存在,而不是在会话期间存在。
单例的想法是在所有包含的脚本中提供对同一对象的访问,而不必使用任何显式的初始化逻辑。
因此,第一次调用
$foo = MyObject::singleton()
会创建一个新的MyObject
,但第二次调用只会返回该对象,而不是创建新对象。这对于访问外部资源(例如数据库和文件)的类非常有用。Singletons exist for the duration of the request, not the duration of the session.
The idea of a singleton is to provide access to the same object across all included scripts, without having to use any explicit initialisation logic.
So, the first call to
$foo = MyObject::singleton()
creates a newMyObject
, but the second call will simply return that object instead of creating a new one. This is incredibly useful for classes that access external resources such as databases and files.部分地。由于 PHP 没有与 ASP.NET 类似的应用程序变量,因此 PHP 中的对象与请求一样长,除非序列化(例如在会话中)。
作为问题的解决方案:将验证码代码(或验证码类,恕我直言,这有点过分了)保存在会话变量中,例如
$_SESSION['captcha']
。Partially. Since PHP has no ASP.NET alike application variables, objects in PHP live as long as the request does, unless serialized (for example in a session).
As a solution to your problem: save the captcha code (or the captcha class, which is a bit of overkill imho) in a session variable, like
$_SESSION['captcha']
.不,不是。
当代码执行结束时,您必须序列化单例对象并将其存储到会话中。当显示下一页时,您可以从会话中取消序列化该对象。
当您将对象分配给会话时,PHP 会自动序列化/反序列化对象。
这仅在您的单例不使用外部资源的链接标识符的前提下才能正常工作。
以下是取自 PHP 文档中的注释
No, it's not.
You'd have to serialize your singleton object and store it to the session when your code execution ends. When the next page is displayed, you can unserialize the object from your session.
PHP serializes/unserializes objects automatically when you assign them to a session.
This only works correctly under the precondition that your singleton does not use link identifiers to external resources.
Here is an example implementation taken from the comments in PHP docs