在 PHP 中实现自己的会话管理

发布于 2024-07-29 07:47:52 字数 250 浏览 10 评论 0原文

有哪些选项可以在 PHP 中实现自己的会话管理?

有没有一种好方法来实现执行所有会话任务的组件? 我如何构建一个在一个请求过程中接收 HTTP 请求和响应的类? 我只找到了选项“session_save_handler”,它似乎定义了存储处理程序。 我需要的是更换整个会话管理。 是否有另一种方法使用 PHP 配置,或者我是否必须实现自己的控制器来接收所有请求并调用我的会话管理?

感谢您的帮助

问候迈克尔

What options are there to implement an own session management in PHP?

Is there a nice way to implement a component which performs all session tasks?
How can i construct a class which receives the HTTP Request and Response during one request process?
I only found the option "session_save_handler" which seems to define the storage handler. What I need is to replace the whole session management.
Is there another way using the PHP configuration or do I have to implement my own controller which receives all requests and calls my session management?

Thanks for your help

Regards Michael

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

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

发布评论

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

评论(6

西瑶 2024-08-05 07:47:52

不,我很遗憾地说,没有界面可以将内置“模块”切换为您自己的。 有一些钩子(例如: session_save_handler(), set_error_handler() ),不幸的是就是这样。

$_SESSION 是一个“超级全局”,如果您正在处理更大的项目,则 IMO 不应该以任何方式直接设置。 那么最好使用负责处理会话的自定义类。 将使代码更容易调试等等。

No, I'm sorry to say, there is no interface to switch the built in 'modules' to your own. There are some hooks ( e.g: session_save_handler(), set_error_handler() ), and that's unfortunately it.

The $_SESSION is a 'super global' and should IMO not be set directly either way if you're working on a bigger projects. Then it would be better to use a custom class responsible for handling sessions. Will make the code easier to debug and such on.

不回头走下去 2024-08-05 07:47:52

我不确定你想要实现什么。 看起来更像是您想要从 $_SESSION 变量中抽象出来,而不是想要更改存储。

看一下 Zend 或 Solar 框架处理会话访问的方式。

http://www.phpeveryday.com/articles/Zend-框架-会话-简介-P571.html
http://solarphp.org/manual:sessions

如何构造一个在一次请求过程中接收 HTTP 请求和响应的类?

我不知道接收响应是什么意思,但是框架有前端/页面控制器,它们路由到所选操作,然后调用可以访问会话(读/写)和请求(读)的方法对象并生成一个 Response 对象,然后通过模板呈现该对象。

对于自动测试,您可以构造自己的 Request 和 Session 对象并将它们传递给页面控制器。

I am not sure, what you want to achieve. It seems more like you want to abstract away from the $_SESSION variable than that you want to change the storage.

Take a look at the way the Zend or the Solar framework handle the Session access.

http://www.phpeveryday.com/articles/Zend-Framework-Session-Introduction-P571.html
http://solarphp.org/manual:sessions

How can i construct a class which receives the HTTP Request and Response during one request process?

I don't know, what you mean by receiving the response, but the frameworks have front-/page-controllers which route to the chosen action, then call a method that can access the Session (read/write) and Request (read) objects and generates a Response object which is then rendered through a template.

For automatic testing you can construct your own Request and Session objects and pass them to the page controller.

怼怹恏 2024-08-05 07:47:52

您自己在其中一篇评论中说过。 只需将 $_SESSION 包装在一个类中即可。 我不认为你可以取代它,但你当然可以为它构建你自己的界面。

例如,你可以。 首先构建一个类,然后在构造函数中调用 session_start()

You said it yourself in one of the comments. Just wrap the $_SESSION in a class. I don't think you can replace it, but you can certainly build your own interface to it.

You could, for example. Build a class that is constructed first thing and call session_start() inside the constructor

怀中猫帐中妖 2024-08-05 07:47:52

使用 session_save_handler() 函数可以处理会话信息的存储和检索方式。

默认情况下,PHP 将会话信息存储在位于 Web 服务器上某处的临时文件中。 您可以使用 session_save_handler() 函数定义回调函数,您可以将此信息存储在数据库表中。

即使您通过 session_save_handler() 函数使用自己定义的函数处理会话,您仍然可以使用超全局变量 $_SESSIONS 访问信息。

Using the session_save_handler() function allows to handle how the session information is stored and retrieved.

By default PHP stores the session information in temporary files located somewhere on your web server. You can define callback functions using the session_save_handler() function where you can have this information stored in a database table instead.

Even if you handle sessions with your own defined functions with the session_save_handler() function you would still access the information with the superglobal variable $_SESSIONS.

骄兵必败 2024-08-05 07:47:52

Check out this page from the php online manual. Has lots of useful information with regards to your question. Hope it helps.

や莫失莫忘 2024-08-05 07:47:52

您可以使用 cookie 和数据库创建会话实现。 您在客户端的机器上设置了一个 cookie。 然后,您在数据库上运行查找,如下所示:

+--------+------+
| sessid | data |
+--------+------+

其中 sessid 包含对 cookie 的引用(可能是 md5SHA 的某个国王> hash),数据类似于 JSONSerialized 数组。

函数:

您可以使用函数 runkit_function_redefine(),该函数是 Runkit API 的一部分,用于重新定义会话_xxxx 函数。
注意:Runkit 是 PECL 的一部分。 也就是说,不与 PHP 捆绑在一起。 您必须自己安装

会话变量:

$_SESSION = &SessionClass->data;

本身很简单:只需将 $_SESSION 作为对您的数据的引用即可。

You could create a session implementation with cookies and a database. You set a cookie on the client's machine. Then, you run a lookup on a database, something like this:

+--------+------+
| sessid | data |
+--------+------+

Where sessid contains a reference to the cookie (probably some king of md5 or SHA hash), and data is something like a JSON or Serialized array.

The functions:

You can use the function runkit_function_redefine(), which is part of the Runkit API, to redefine the session_xxxx functions.
Note: Runkit is part of PECL. That is, NOT BUNDLED WITH PHP. You will have to install it yourself.

The session variable:

$_SESSION = &SessionClass->data;

Simplicity itself: just make $_SESSION as a reference to YOUR data.

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