2013-07-23 44 views
0

我在DB中有一張表,我把所有的管理員和他們的權利。我使用字母A-Z設置theri權限,每封信都可以訪問其他內容。事情是我想檢查他們有權訪問的地方,並只顯示那些地區的鏈接。現在我有一個想法,把所有這些字母在一些aray for循環,然後當呈現菜單輸出只有那些他們有訪問的項目與if (in_array(letter, rights))我認爲這將工作,但我想知道是否有另一種方式PHP單獨訪問某些鏈接

+1

我不會用字母,因爲它是難以維持「嗯爲用戶管理提供訪問權限......是「M」還是「Q」? –

+0

這個想法是,我會在管理選項添加管理員,並會有複選框的字母作爲值。 – DeiForm

+0

這個問題可能也是有用的閱讀:http://stackoverflow.com/questions/199252/what-is-the-best-way-to-manage-permissions-for-a-web-application-bitmask-or- da –

回答

1

我寫了一個PHP類,我用在所有需要不同角色訪問的項目中。我把這個類的副本放在了paste bin:class_role_restrictions.php

此類還需要另一個類我寫的,可以在這裏獲得:better_mysqli.php

初始設置涉及到一些數據庫表的創建(在SQL創建語句在評論在class_role_restrictions.php的結尾處找到文件),並通過它的管理界面添加角色/用戶成員資格。

詳細設置/可以在這裏獲得的使用:role_restrictions_detailed_example.php

一旦安裝就可以使用這樣的:

<?php 

    include_once('class_role_restrictions.php'); 
    include_once('better_mysqli.php'); 


    $mysqli = new better_mysqli('your_server', 'your_user', 'your_pass', 'your_db_name'); 
    if (mysqli_connect_errno()) { 
    error_log(sprintf("Can't connect to MySQL Server. Errorcode: %s\n", mysqli_connect_error())); 
    exit; 
    } 


    $role = new role_restrictions($mysqli, 'http://your_server.com/path/to/this/page/example.php'); 



    // == Restrict an entire page to authorized users only == 
    $role->restrict_to('role_name, to_restrict user_to', 'username_currently_logged_in'); 
    // .. now do stuff that only users in any of the roles listed are allowed to do 

    // -- OR -- 

    // == Only do certain things if the username given is a member of any of the roles given == 
    if($role->has_role('a_list, of_roles, allowed', 'username_currently_logged_in')){ 
     // .. do stuff that only users in the given role(s) are allowed to do .. 
    } 


?>