2011-04-02 223 views
5

我有一個PHP網站有多個PHP腳本。我需要從另一個站點向我的系統提供有限的訪問權限。我想限制這些ppl可以訪問的頁面。限制訪問限制訪問用戶的PHP腳本

我以下列方式這樣做:

// $_SESSION['systemid'] is set with a value of say, '1' 
$permissionArray = $objACCESS->getPermissions($_SESSION['systemid']); 

// getPermissions returns an array like the following (for that systemid): 
// 0 => {'systemid' => '1', 'permission_type' => 'createcontent' } 
// 1 => {'systemid' => '1', 'permission_type' => 'invitecontacts' } 

// the following contain a list of script names that should be 
// restricted if permission is not allowed 
$createcontent = array('createcontent.php'); 
$managecontent = array('managecontent.php'); 
$invitecontacts = array('invitecontacts.php'); 

$page_name=basename($_SERVER["SCRIPT_FILENAME"]); 

if(is_array($permissionarray)) 
{ 
    $haspermissions = false; 
    foreach($permissionarray as $permissions) 
    { 
     if(in_array($page_name,${"$permissions[permission_type]"})) 
     { 
      $haspermissions = true; 
      break; 
     } 
    } 
} 

if($haspermissions==false) 
{ 
    // - do not have permissions 
    echo "<meta http-equiv=\"refresh\" content=\"0;url=".$site_url."404.php\">"; 
    die; 
} 

... 
// rest of the code 
... 

Q1:是否有限制用戶的訪問更好的辦法? Q2:如果不是,有沒有辦法讓這種方法更有效率/最優?

+0

+1很好的清晰問題。請注意,但看起來類似於:http://stackoverflow.com/questions/1392428/restricting-access-to-a-site-using-ip-address – Smandoli 2011-04-02 18:05:39

+0

「我需要提供來自其他網站的用戶有限訪問」這不是明確? 「另一個網站」下的含義是什麼?反正+1! – Wh1T3h4Ck5 2011-04-02 18:11:32

+0

感謝您的評論smandoli - 但我正在尋求限制這個「外部系統」用戶在我的網站上游蕩 - 這種限制是基於他來自哪個系統,而不是他的IP地址/地理位置 – siliconpi 2011-04-02 18:12:14

回答

0

這裏的底層認證機制對我沒有意義。 $ _SESSION ['systemid']如何設置?什麼是「系統」?

無論如何,我會假設你已經弄清楚了這部分問題。因此,我會修改你把上面如下:

首先,調整getPermissions返回類似:

$perms = array(
    'createcontact' => 1, 
    'invitecontacts' => 1 
); 

這個數組將只與該「系統」相關的權限填充。

然後,檢查當前的「系統」有一個頁面所需的權限如下:

$cur_page_perm_key = basename($_SERVER['SCRIPT_FILENAME'], '.php'); 
$has_permission = isset($perms[$cur_page_perm_key]); 
if(!$has_permission) { 
    // No permission? Redirect to an unauthorized page 
    header('HTTP/1.0 401 Not Authorized'); 
    header('Status: 401 Not Authorized'); 
    header('Location: /unauthorized.php'); 
    exit; 
} 

簡單的「isset」檢查將是一個很多比循環快,特別是如果許可數量/頁面增長。

希望這會有所幫助。

+0

嗯 - 我認爲這會工作得很好 - 謝謝! – siliconpi 2011-04-04 04:10:09