我想要一些有php经验的人的建议。
我正在制作一个php网站,它将有4种类型的用户:1.客人(未注册),2.注册,3.以特殊特权注册,4.管理员
因此,同一个页面对他们四个人的可视性将有所不同。
现在,我使用if
条件来实现这一点。在每个页面中,我都会检查用户的role
,然后使用许多if
语句来相应地显示页面。
它使代码变得非常大和不整洁,我不得不一次又一次地检查所有页面中的条件。
-
有更好的方法吗?
-
大型专业网站是如何做到这一点的?
-
扩展问题:使用像kohana 3.1这样的MVC框架,做同样的事情的最佳方式是什么?这和
acl
有关系吗?
这实际上取决于您需要什么。
例如,如果页面有很大一部分完全更改,我建议创建不同的模板,并根据它们的"权限"将其包含在中
$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",则需要创建一个函数