对具有不同角色的用户显示不同的页面

时间:2022-10-13 12:45:23

I wanted some suggestions from someone with experience in php.

我想要一些有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

我在php中建立一个网站,有4种用户:1。访客(未注册),2。注册,3。注册特殊权限,4。管理员

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.

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

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?

    扩展问题:使用像kohana 3.1这样的MVC框架,最好的方法是什么?它与acl有什么关系吗?

1 个解决方案

#1


5  

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

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

#1


5  

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

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