简体   繁体   English

php - 如何在面板中为管理员提供自己的部分?

[英]php - how to give an admin their own section in panel?

I have a little problem with my user/login panel in php. 我的用户/登录面板在php中有点问题。 If guest comes to see my page in his panel he have options for login and register. 如果客人在他的面板中看到我的页面,他可以选择登录和注册。 Users see their member panel like below: 用户看到他们的成员面板如下:

(there are some IF functions, which making sure that there's no session id in base, then showing options for guest) (有一些IF函数,确保在base中没有会话ID,然后显示guest的选项)

                  <?php
        else:
        ?>

<div> 
<h1>Members panel</h1>
<p>You can put member-only data here</p>
<p><a href="registered.php">View a member page</a></p>           
<p><a href="changepass.php">Edit your password</a></p>
<p>-----</p>
<a href="?logoff">Log off</a>

Now: in my database with users i have column named ROLE. 现在:在我的数据库中,用户我有一个名为ROLE的列。 There are roles like user and admin. 有用户和管理员等角色。 I want to make admins see another section in that panel. 我想让管理员看到该面板中的另一部分。 I was trying with another switch/case and IF instruction, like: 我正在尝试使用另一个开关/案例和IF指令,例如:

                  <?php
                  if (isset($_SESSION['role']) && ($_SESSION['role'] =='admin')); 
                  {    
                  }
                  ?>
                  (there is member panel) 
                  <div class="left right">      
        <h1>Administrator panel</h1>
        <a href="admin.php">View an admin page</a>

but it doesn't work. 但它不起作用。 (I have session variable role.) Any ideas? (我有会话变量角色。)有什么想法吗?

You may try by removing the ';' 您可以尝试删除';' after if 之后如果

<?php
  if (isset($_SESSION['role']) && ($_SESSION['role'] =='admin')) {    
?>
<!--ADMIN PANEL GOES HERE-->
<?php
    } else {
?>
<!---USER PANEL GOES HERE-->
<?php
    }
?>

first of all you should seperate view and control, ie do not put php code in your html files. 首先你应该分开查看和控制,即不要将PHP代码放在你的html文件中。 there are plenty of possibilities to keep this much cleaner. 有很多可能性来保持这个更清洁。 the SMARTY framework for instance is quite popular. 例如, SMARTY框架非常受欢迎。

second, how is the role written to your $_SESSION (and why?). 第二,如何将角色写入$ _SESSION(以及为什么?)。 you should better pass your db-recordset. 你应该更好地传递你的db-recordset。 pseudo: 伪:

$rs = db->query(SELECT * FROM users ORDER BY userID)
if ($rs[&currentUserID]['role'] == 'admin') {
  ...
}

third: if you mess with php and html you should at least generate your php in a seperate area (obviously before any output) and output your HTML for instance with heredoc syntax: 第三:如果你搞乱php和html,你至少应该在一个单独的区域(显然在任何输出之前)生成你的php,并输出你的HTML,例如heredoc语法:

<?php
  // do any PHP stuff here
  $panel = 'user logged in';
  if (admin) {
    $panel .= "i'm an admin";
  }
echo <<<OUT
<!DOCTYPE html>
<html>
  ... // put all HTML in here and use variables

  <body>
    <div>$panel</div>
  </body>
</html>
OUT;
?>

PS: please remove the css tag as your question has nothing to do with it PS:请删除css标签,因为你的问题与它无关

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM