2015-11-14 64 views
0

我有登錄頁面和用戶頁面,用戶使用正確的用戶名和密碼登錄後,會進入用戶頁面。 用戶頁面由用戶列表組成,並在每個用戶行旁邊都有一個「編輯用戶」按鈕。例如,在這裏的截圖 - > http://i.stack.imgur.com/K3qxi.pngPHP - 根據用戶級別隱藏/顯示內容

因此,基於用戶級別,如果用戶級別=用戶(其具有ID = 4),我想隱藏的編輯按鈕。到目前爲止,我嘗試過的是這樣的,但它不起作用。我已導入會話代碼和所有查詢。下面的代碼只是我想要做的一部分。

include_once("session.php"); 
if($_SESSION['userlvl']==4){ 
    echo "USER"; 
?> 

<script> 
$document.(ready(function){ 
    $(#editbutton).hide(); 
}); 
</script> 

<?php 
} 
?> 


<a id="editbutton"><img src="Edit.png"/></a> 
+0

它是如何不工作?你有錯誤嗎? – Andrius

+1

你應該使用PHP而不是JavaScript來隱藏它,否則你仍然可以在源代碼中找到它。您還應該保護編輯功能和接收表單的腳本,因爲即使用戶沒有該按鈕,他們仍然可以向該頁面發送請求(如果他們知道URL和表單)。 – insertusernamehere

回答

-1

這是一個簡單的解決方案。

include_once("session.php"); 
if ($_SESSION['userlvl'] == 4) { 
    $hideme = 'display:none;' 
} else { 
    $hideme = ''; 
} ?> 


< a id = "editbutton" 
style = "<?php echo $hideme; ?>" > < img src = "Edit.png"/> < /a> 
+0

其工作,謝謝! :) –

0

,你可以用它代替ID級別

include_once("session.php"); 
if($_SESSION['userlvl']==4){ 
    echo "USER"; 
?> 

<script> 
$document.(ready(function){ 
    $(".editbutton").hide(); 
}); 
</script> 

<?php 
} 
?> 


<a class="editbutton"><img src="Edit.png"/></a> 
+0

其仍然不工作,但謝謝btw :) –

0

正如由@insertusernamehere你應該使用PHP來控制什麼是寫入根據用戶的訪問級別的頁中指出 - 僅僅從視圖中隱藏內容只是要求麻煩,因爲在代碼視圖中查找時很容易找到。以下只是你如何做到這一點的一個例子。

<?php 
    include_once("session.php"); 
?> 
<html> 
    <head> 
     <title>Restrict content based upon user level example</title> 
     <?php 
      if(isset($_SESSION['userlvl'])){ 

       $level=intval($_SESSION['userlvl']); 
       /* 
        If you have javascript functions that actually submit the forms or 
        interact somehow with the data that are to be restricted to particular 
        levels of user, generate the code for that level only. 
       */ 
       switch($level){ 
        case 1: 
         /* Basic user */ 
         echo " 
          <script id='user' type='text/javascript'> 
           function view_record(){ 
            alert('hello world'); 
           } 
          </script> 
         "; 
        break; 
        case 2: 
         /* Superuser */ 
        break; 

        case 3: 
        case 4: 
         /* Manager & Admin level users ? */ 
         echo " 
         <script id='admin' type='text/javascript'> 
          function delete_all_users(){ 

          } 
          function drop_tables(){ 

          } 
          function drop_database(){ 

          } 
          function edit_record(e){ 
           alert(e+' edit record') 
          } 
          function delete_record(e){ 
           alert(e+' delete record') 
          } 
         </script> 
         "; 
        break; 
       } 
      } 
     ?> 
    </head> 
    <body> 
     <table> 
     <?php 
      /* Assuming you use php to generate the table: pseudo style code */ 
      while($rs=$result->fetch()){ 
       $editbttn=''; 
       $deletebttn=''; 

       if(in_array($level, array(3,4))){ 
        /* Admin & manager */ 
        $editbttn="<a class='editbutton' onclick='edit_record(event)'><img src='Edit.png'/></a>"; 
        $deletebttn="<a class='deletebutton' onclick='delete_record(event)'><img src='Delete.png'/></a>"; 
       } 

       echo " 
        <tr> 
         <td>DATA</td> 
         <td>DATA</td> 
         <td>DATA</td> 
         <td>$editbttn</td> 
         <td>$deletebttn</td> 
        </tr>"; 
      } 
     ?> 
     </table> 
    </body> 
</html> 
0

我認爲你想限制只有「用戶」權限的用戶不能編輯數據?

你可能想考慮嘗試一下,因爲我認爲它更直接簡單?

session_start(); 
if($_SESSION['userlvl'] == 4) { 
    echo "USER"; 
    }else{ 
    $userlvlpermission = '<a id="editbutton"><img src="Edit.png"></a>'; 
    } 
//At where you wanted to place the "editbutton" 
<?php if($userlvlpermission){echo $userlvlpermission;}?> 

確保您檢查編輯頁面的用戶會話,以防萬一他們知道確切的網址。

AT編輯頁面

<?php 
session_start(); 
if ($_SESSION['userlvl'] = 4){ 
echo "NO PERMISSION"; 
echo "header('Refresh: 3;url=page.php')"; 
exit(); 
} 
+0

我試過這種方法,但有一個錯誤說$ userlvlpermission未定義。順便說一句,如果我檢查編輯頁面的用戶會話,如果用戶級別是用戶,如何禁用表單?我可以讓表單禁用嗎? –

+0

我想你有一個登錄頁面。您可以重定向登錄頁面或回顯「無權編輯」並退出腳本,請參閱編輯代碼 –

+0

好的,我會嘗試。謝謝:) –