我正在使用下面的代碼來檢查登錄級別 - 創建必須具有某個訪問級別才能訪問的頁面的最優雅方式是什麼?PHP函數創建user_level特定頁面
function check_login($level) {
$username_s = mysql_real_escape_string($_SESSION['username']);
$sql = "SELECT user_level, restricted FROM login_users WHERE username = '$username_s'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$user_level = $row['user_level'];
$restricted = $row['restricted'];
$sql = "SELECT level_disabled FROM login_levels WHERE level_level = '$user_level'";
$result = mysql_query($sql);
$row2 = mysql_fetch_array($result);
$disabled = $row['level_disabled'];
if($disabled != 0) { include('disabled.php'); exit();
} elseif($restricted != 0) { include('disabled.php'); exit();
} elseif($user_level <= $level) { // User has authority to view this page.
} else { include('user_level.php'); exit();
}
}
您可能會發現本教程很有用:[Flat PHP遇到Symfony時](http://symfony.com/doc/current/book/from_flat_php_to_symfony2.html)它顯示瞭如何分離應用程序的各個部分併爲頁面提供訪問控制。 – hakre