我有這樣的代碼PHP目錄的具體內容和重定向
$pageEx = explode("/", $_SERVER['PHP_SELF']);
$pageLn = count($pageEx);
$currentdir = $pageEx[$pageLn - 2];
switch($currentdir) {
case "admin":
if(!$this->loggedIn) {
header("Location: index.php");
}
if($this->userData['user_level'] < 3) {
header("Location: ../index.php");
}
break;
case "mgmt":
if(!$this->loggedIn) {
header("Location: index.php");
}
if($this->userData['user_level'] < 2) {
header("Location: ../index.php");
}
break;
case "user":
if(!$this->loggedIn) {
header("Location: index.php");
}
if($this->userData['user_level'] < 1) {
header("Location: ../index.php");
}
break;
}
和只是想知道是否有一個較短的方式,我能做到這一點?代碼有效,但它的很多代碼非常簡單。
它檢查它們在的目錄,然後如果它們不是正確的user_level,它將它們重定向到索引頁面。
編輯:完成它。
$pageEx = explode("/", $_SERVER['PHP_SELF']);
$pageLn = count($pageEx);
$currentdir = $pageEx[$pageLn - 2];
/*
User Level Required => Directory
*/
$permissions = array(
1 => 'user',
2 => 'mgmt',
3 => 'admin'
);
foreach($permissions as $perms => $key) {
if(!$this->loggedIn) {
header("Location: ../index.php");
}
if($currentdir == $key) {
if($perms > $this->userData['user_level']) {
header("Location: ../index.php");
}
}
}
爲什麼要在foreach的每一次迭代中解析'!$ this-> loggedIn'?在foreach之前和之前移動它。 – 2011-04-21 01:19:32