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

Par*_*ang 10 php kohana kohana-3

我想要一些有PHP经验的人的建议.

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

因此,对于所有这四个页面,同一页面将以不同方式显示.

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

它使代码非常大且不整洁,我必须在所有页面中反复检查条件.

  1. 有一个更好的方法吗?

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

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

Fab*_*zio 5

这真的取决于你需要什么.

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

 $permission = $_SESSION['type_user'];
 include '/path/to/file/with/permission/'.$permission.'/tpl.html';
Run Code Online (Sandbox Code Playgroud)

并在页面中有类似的东西

<?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';
?>
Run Code Online (Sandbox Code Playgroud)

如果脚本中包含"显示此文本"或"显示此按钮"等小部件,则可以创建一个将为您检查权限的函数

<?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]
Run Code Online (Sandbox Code Playgroud)

根据经验,如果一段代码使用次数超过2次,则需要单独放入函数/文件中,因此如果你有很多"IFS",你需要创建一个函数

  • 谢谢。虽然我花了一些时间来理解,但我明白了。知道大型专业网站是如何做到的吗? (2认同)