Forum Tutorial - User Levels #2 - Moderator Privileges and Admin Panel
Submitted by Yorkiebar on Tuesday, March 4, 2014 - 05:51.
Introduction:
This tutorial is carrying on from my previous tutorial of User Levels #1 on a Forum Creation Tutorial Series. Since this is #2, we are setting the permissions and actions for Moderators and creating the admin panel.
Moderators:
Because we will need to check the user level of the logged in user each time we want to check if they have privileges over other users, we are going to set a new session variable on the login script (isMod)...
Now we can simply check if 'isMod' is set in the session variables when we want to check if the logged in user is a moderator. So next, when we output each thread on our main home page we want to check if the logged in user ia moderator, if they are, give them a delete option...
We also need to alter our delete script to check that the person trying to delete the post is either the author of the post or a moderator...
Next we need to add the option next to replies to a thread...
Next we will list each user in the users table within our database along with their current user level, and a small form to change their user level to a new number...
Next we process the forms by checking the GET and POST arguments given by the forms. We check the level is between 1 and 3, otherwise we output there's a problem and reset it accordingly...
In the above code we also make sure the user exists before we attempt to alter their user level (because this would cause an error, obviously).
Next tutorial we will be finishing off admin privileges which includes banning users.
- $pass = $_POST['pass'];
- $user = $_POST['user'];
- $storedPassword = $info['password'];
- if ($storedPassword == $passMD5) {
- $_SESSION['username'] = $user;
- if ($info['level'] == 2)
- $_SESSION['isMod'] = 'yes';
- echo 'Logged in!';
- }else
- echo 'Password was incorrect. Please try again.';
- }else
- echo 'That username was not found. Please try again.';
- }
- echo '<tr><td><a href="threadPage.php?tid='.$row["id"].'">'.$row["title"].'</td><td>'.$content.'...</td>';
- echo '<td><a href="threadPage.php?act=delete&type=thread&id='.$row["id"].'">Delete</td>';
- echo '</tr>';
- if ($delType == 'reply') {
- $delAuthor = $delAuthorInfo['author'];
- }else
- echo 'You do not have permission to do that!';
- }else if ($delType == 'thread') {
- $delAuthor = $delAuthorInfo['author'];
- }else
- echo 'You do not have permission to do that!';
- }
Admin Panel:
Next we are going to make the admin panel. Here admins will be able to change other users' levels. Since the next tutorial is more on admin features, we are not going to add a check to see if the user accessing the page is an admin, you cna add it if you know how, send me a message if you need help, or read the next tutorial.
Before we can do anything, make a new file named "admin.php", this is where the panel will go.
So first we are going to connect to the database and continue our sessions...
- <?php
- ?>
- <html>
- <head></head>
- <body>
- </body>
- </html>
- <h1>Users List:</h1>
- <table>
- <tbody>
- <?php
- echo '<tr><td>'.$row["username"].'</td><td>Current Level: '.$row["level"].'</td><td>Change Level: </td><td><form action="admin.php?act=level&uID='.$row["id"].'" method="POST"><input type="number" name="newLevel" /><input type="submit" value="Change User Level" /></form></td></tr>';
- }
- ?>
- </tbody>
- </table>
- $userID = $_GET['uID'];
- $level = $_POST['newLevel'];
- if ($level > 3) {
- echo 'Warning, new user level is above three, resetting to three.';
- $level = 3;
- }else if($level < 1) {
- echo 'Warning, new user level is below one, resetting to one.';
- $level = 1;
- }
- //User exists.
- if ($qq) {
- echo 'Updated user level.';
- }else
- echo 'Failed to update user level.';
- }
- echo 'Redirecting.';
- }
Add new comment
- 198 views