我创建了一个页面,其中调音师(第 2 组)可以拥有自己的页面来显示正在发生的事情、被禁止的用户等。我希望管理员(组 1)也能够访问该页面。im 使用的函数是:
function mod_protect() {
global $user_data;
if (has_access($user_data['user_id'], 1, 2) === false) {
header('Location: index.php');
exit();
}
}
当我只使用 mods 组号 (2) 时,它工作正常,但当我尝试将两者放在 1 个作品中时??
对不起has_access代码:
function has_access($user_id, $type) {
$user_id = (int)$user_id;
$type = (int)$type;
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_id` = $user_id AND `type` = $type"), 0) == 1) ? true : false;
}
我可以建议将其作为has_access()
函数的替代方法吗:
function has_access($user_id, $type) {
$user_id = (int)$user_id;
if( is_array( $type ) {
$types = implode(',',$type);
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_id` = $user_id AND `type` IN ($types)"), 0) == 1) ? true : false;
else {
return (mysql_result(mysql_query("SELECT COUNT(`user_id`) FROM `users` WHERE `user_id` = $user_id AND `type` = $type"), 0) == 1) ? true : false;
}
}
这样做允许您将单个类型传递给 has_access()
函数或将类型数组传递给同一函数。
根据您的代码,您可以这样做(尽管当一个可以轻松使用时,它会使用两个查询 - 不过这是您做的优化):
if (has_access($user_data['user_id'], 1) === false && has_access($user_data['user_id'], 2) === false) {
header('Location: index.php');
exit();
}