Forum Tutorial - User Levels #3 - Admin Privileges, Banning Users & Logging Out
Submitted by Yorkiebar on Wednesday, March 5, 2014 - 06:44.
Introduction:
This tutorial is the third and final episode to the User Levels section of my Forum Creation Tutorial series. In this part, we are going to be adding banning functionality and giving the admins their privileges.
Admin Privileges:
This part is essentially the same as the moderator privileges part found the previous tutorial. Once the user has logged in using our login form found in login.php we check if the user level of that user is 3 (admin), if it is we set the isAdmin session variable...
Next, we go to the checks for moderators and add the exception for admins so admins too can delete posts by other users. So add:
to threadPage.php replies, and forumTutorial.php (index/thread list page)...
Finally we want to add a check just under where we continue our session and connect to our database on the admin panel page to check that the user accessing the page is an admin...
Finally we need to check on login if the user is banned, if they are then we give an error otherwise we allow them to login as normal...
We might want an option to unban users as well so we simply do the opposite...
Logout Script:
I thought I had already covered logging out scripts, but I don't seem to have a logout page in my directory. The script is a simple two line php script and simply connects to the session then destroys it ready for a new one the next time another script connects...
- if ($info['level'] == 3)
- $_SESSION['isAdmin'] = 'yes';
- if ($delType == 'reply') {
- $delAuthor = $delAuthorInfo['author'];
- if (strtolower($delAuthor) == strtolower($_SESSION['username']) || isSet($_SESSION['isMod']) || isSet($_SESSION['isAdmin'])) {
- }else
- echo 'You do not have permission to do that!';
- }else if ($delType == 'thread') {
- $delAuthor = $delAuthorInfo['author'];
- if (strtolower($delAuthor) == strtolower($_SESSION['username']) || isSet($_SESSION['isMod']) || isSet($_SESSION['isAdmin'])) {
- }else
- echo 'You do not have permission to do that!';
- }
- $replies .= '<tr><td>'.$row["content"].'</td><td>'.$author.'</td><td>'.$repliedUser["signature"].'</td>';
- $replies .= '<td><a href="threadPage.php?act=delete&type=reply&id='.$row["id"].'">Delete</a></td>';
- $replies .= '</tr>';
Banning Users:
Next we want to add the ability for admins to ban users. We could add a new column in to our database to hold their current ban status (0 or 1, false or true, unbanned or banned) but instead we are going to use their level as 0 if they are banned.
First we add another option next to the change level option for every user within the admin panel...
Next we need to process when the get parameter of act is ban, we grab the user id from the url as well (through PHP GET statements) then we set their user level to 0...
(Appending on to the end of the if statement script which checks for the act level parameters):
- $uID = $_GET['uID'];
- //User exists, set level to 0 and ban them.
- if ($banUserQuery) {
- echo 'Banned user.';
- }else
- echo 'Failed to ban user...';
- }
- }
- $pass = $_POST['pass'];
- $user = $_POST['user'];
- if ($info['level'] != '0') {
- $storedPassword = $info['password'];
- if ($storedPassword == $passMD5) {
- $_SESSION['username'] = $user;
- if ($info['level'] == 2)
- $_SESSION['isMod'] = 'yes';
- if ($info['level'] == 3)
- $_SESSION['isAdmin'] = 'yes';
- echo 'Logged in!';
- }else
- echo 'Password was incorrect. Please try again.';
- }else
- echo 'That account is banned!';
- }else
- echo 'That username was not found. Please try again.';
- }
- $uID = $_GET['uID'];
- //User exists, set level to 1 and unban them.
- if ($banUserQuery) {
- echo 'Unbanned user.';
- }else
- echo 'Failed to unban user...';
- }
- }
- <?php
- ?>
Comments
Logout bug
Hi - the logout script doesn't seem to work ? I used your login script for my site as well, and when I clicked on 'Logout' (Which was linked to logout.php) nothing happened
Add new comment
- Add new comment
- 182 views