来自会话php的基于用户的内容

时间:2017-09-28 14:54:17

标签: php mysqli

我正在为自定义用户脚本设置一个组字段。我怎样才能向某些群体展示某些内容

示例:

admin (all content)
moderator (extended content)
user (user content)
guest (general overview)

4组ID为0 - 4

4 banned
3 admin
2 moderator
1 user
0 guest

正在使用的PHP示例               

      // Keep reminding the user this account is not active, until they activate
     if($row['group_id'] == 4) { //display all
          echo
          '<div class="info">
          This account is at risk of being banned Please obey the site rules.
          </div>';
      } else {
          exit();
      }

PHP MySQL会话的示例

/* Displays user information and some useful messages */
session_start();

// Check if user is logged in using the session variable
if ( $_SESSION['logged_in'] != 1 ) {
  $_SESSION['message'] = "You must log in before viewing your profile page!";
  header("location: http://localhost/login-system/error.php");    
}
else {
    // Makes it easier to read
    $first_name = $_SESSION['first_name'];
    $last_name = $_SESSION['last_name'];
    $email = $_SESSION['email'];
    $active = $_SESSION['active'];
    $group_id = $_SESSION['group_id'];
}

1 个答案:

答案 0 :(得分:0)

您可以使用此示例执行此操作:

让我们说这是一个博客。

<?php
// 0 = Banned
// 1 = User
// 2 = Admin
if($_SESSION['group_id'] == 0) {
?>
<h1>You Are Banned</h1>
<?php
} elseif($_SESSION['group_id'] == 1) {
?>
<p>Welcome to my blog.</p>
<?php
} elseif($_SESSION['group_id'] == 2) {
?>
<p>Welcome to my blog.</p><button>Delete Post</button>
<?php
}

或者你可以通过第二种方法来做到这一点:

<p id="1">Welcome To My Blog!</p><button id="2">Delete Content</button>
<h1 id="3">You are banned from this website!</h1>
<?php
if($_SESSION['group_id'] == 0) {
echo '<style>
#2 {
display:none;
}
#3 {
display:none;
}
</style>';
} elseif ($_SESSION['group_id'] == 1) {
echo '<style>
#1 {
display:none;
}
#3 {
display:none;</style>';
} elseif ($_SESSION['group_id'] == 2) {
echo '<style>
#1 {
display:none;
}';
}

希望这有帮助!