PHP:集中页面会话验证器

发布于 2024-07-17 07:14:36 字数 203 浏览 4 评论 0原文

我目前想知道如何实现集中式页面验证器。 谁能为我建议一个简洁的算法? 我想要实现的是使我的后端管理员页面会话受到保护,而无需向我想要保护的每个页面编写一段会话检查代码。 我目前正在做这样的事情:

登录页面-> 如果凭据正确:设置会话 -> 如果在没有会话的情况下查看受保护的页面:拒绝,否则:允许

任何最佳实践(或更好的方法)?

I am currently wondering how a centralized page authenticator could be achieved. Can anyone suggest a neat algorithm for me? What I intend to achieve is to make my backend administrator pages session protected without writing a piece of session checking code to each of my pages that I want protected. I currently do something like this:

login page -> if right credentials : set session -> if view protected page without session : reject else : permit

Any best practices (or a better method) on/than this?

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

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

发布评论

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

评论(2

银河中√捞星星 2024-07-24 07:14:36

我不太想建议一种算法,而是建议一个库/框架。

如果您的应用程序有一个入口点,那就是调用会话管理库/框架的地方。 例如,使用 Zend Framework,您可以在引导程序中启动会话。 剩下的唯一事情就是在登录控制器中验证会话。

I would not so much want to suggest an algorithm, but a library/framework instead.

If your application has a single entry point, that is the place to call your session management library/framework. For example with the Zend Framework you can initiate your session in the bootstrap. The only thing left is to authenticate a session in the login controller.

复古式 2024-07-24 07:14:36

如果您正在构建一个具有多个入口点的 PHP 应用程序,您会疯狂地尝试复制和粘贴所有这些代码。 考虑使用真正的 MVC 框架。

我使用 Zend_Controller 在这种情况下路由我的页面浏览量。

一旦以这种方式构建应用程序,向受限控制器的 predispatch() 方法添加一些代码以进行身份​​验证并在未找到或无效时重定向到登录页面就变得很简单。

If you're architecting a PHP app that has multiple entry points you will go crazy trying to copy and paste all this code. Look into using a real MVC framework.

I use Zend_Controller to route my pageviews in situations like this.

Once you architect your app in that way, it becomes simple to add some code to the predispatch() method of your restricted controllers to do authentication and redirect to a login page if it is not found or is invalid.

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