对不同角色的用户以不同方式显示同一页面

发布于 2024-11-17 23:41:33 字数 428 浏览 2 评论 0原文

我想要一些有 php 经验的人的建议。

我正在用 php 制作一个网站,该网站将有 4 种用户: 1. 来宾(未注册), 2.注册, 3. 注册享有特殊特权, 4. 管理员

因此,同一页面对所有四个人来说都是不同的可见方式。

现在我正在通过使用 if 条件来做到这一点。 在每个页面中,我都会检查用户的角色,然后使用许多 if 语句来相应地显示页面。

它使代码变得非常大且杂乱,我必须一次又一次地检查所有页面中的条件。

  1. 有更好的方法吗?

  2. 在大型专业网站中这是如何完成的?

  3. 扩展问题: 使用像 kohana 3.1 这样的 MVC 框架来做同样的事情的最佳方法是什么?它与acl有什么关系吗?

I wanted some suggestions from someone with experience in php.

I am making a website in php which will have 4 kinds of users :
1. guest(unregistered),
2. registered,
3. registered with special privilages,
4. admins

So the same page will be visible differently to all four of them.

Right now I am doing that by using if conditions.
In every page, I am checking the role of the user and then using many if statements to display the page accordingly.

It makes the code very big and untidy and I have to check conditions again and again in all the pages.

  1. Is there a better way to do this?

  2. How is this done in big professional websites?

  3. Extended Question:
    What is the most optimal way to do the same using a MVC framework like kohana 3.1? Does it have anything to do with acl?

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

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

发布评论

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

评论(1

寻梦旅人 2024-11-24 23:41:33

这实际上取决于您的需要。

例如,如果页面有很大的部分完全改变,我建议是创建不同的模板并根据它们的“权限”包含它们

 $permission = $_SESSION['type_user'];
 include '/path/to/file/with/permission/'.$permission.'/tpl.html';

,并且在页面中包含类似于

<?php
//inside include.php you have the line similar to
//$permission = isset($_SESSION['type_user']) && $_SESSION['type_user']!=''?$_SESSION['type_user']:'common';
require_once '/mast/config/include.php';
include '/path/to/file/with/permission/common/header.html';
include '/path/to/file/with/permission/'.$permission.'/tpl_1.html';
include '/path/to/file/with/permission/common/tpl_2.html';
include '/path/to/file/with/permission/'.$permission.'/tpl_3.html';
include '/path/to/file/with/permission/common/footer.html';
?>

脚本充满小部分(例如“显示这个”)的 内容文本”,或“显示此按钮”,您可以创建一个函数来为您检查权限

<?php
function can_user($action, $what){
   switch($action){
      case 'write':
          return $your_current_if_on_what;
          break;
      case 'read':
      default:
          return $your_current_if_on_what;
          break;
   }
}
?>

and the template will look like:

[my html]
<?=can_user('read','button')?'My Button':''?>
[my html]

根据经验,如果一段代码使用超过 2 次,则需要将其放入函数/文件中分开,所以如果你有很多“IFS”,你需要创建一个函数

It really depends on what you need.

For example if the page has big part that change completely, what I would suggest is to create different templates and include them depending on their "permissions"

 $permission = $_SESSION['type_user'];
 include '/path/to/file/with/permission/'.$permission.'/tpl.html';

and have something in the page similar to

<?php
//inside include.php you have the line similar to
//$permission = isset($_SESSION['type_user']) && $_SESSION['type_user']!=''?$_SESSION['type_user']:'common';
require_once '/mast/config/include.php';
include '/path/to/file/with/permission/common/header.html';
include '/path/to/file/with/permission/'.$permission.'/tpl_1.html';
include '/path/to/file/with/permission/common/tpl_2.html';
include '/path/to/file/with/permission/'.$permission.'/tpl_3.html';
include '/path/to/file/with/permission/common/footer.html';
?>

if the script is full of small parts like "show this text", or "show this button", you can create a function that will check the permissions for you

<?php
function can_user($action, $what){
   switch($action){
      case 'write':
          return $your_current_if_on_what;
          break;
      case 'read':
      default:
          return $your_current_if_on_what;
          break;
   }
}
?>

and the template will look like:

[my html]
<?=can_user('read','button')?'My Button':''?>
[my html]

As a rule of thumb, if a piece of code is used more than 2 times, it needs to be put in a function/file separately, so if you have many "IFS" you need to create a function

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