Forum Tutorial - MOTD and Banning Messages

Introduction: This tutorial is going to be covering message of the days and banning messages. Database Setup: First we are going to set the database up ready for storing our information, create a new table named "reasons", then give it the following columns of structure: id - INT - 5 Length - Primary Key - Auto Increment (AI/A_I) use - VARCHAR - 255 Length message - VARCHAR - 255 Length additional - VARCHAR - 255 Length We are going to make the use value "motd" for Message of the Day, or "ban" for the banning message. While we use the additional slot to hold the username for banning messages. Banning Messages: Next we are going to add a banning message option to the banning form of the admin panel...
  1. 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><td><form action="admin.php?act=ban&uID='.$row["id"].'" method="POST"><input type="text" name="message" /><input type="submit" value="Ban User!" /></form></td><td><form action="admin.php?act=unban&uID='.$row["id"].'" method="POST"><input type="submit" value="Unban User!" /></form></td></tr>';
Next we will alter our processing to add the banning reason to our new table...
  1. else if(isSet($_GET['act']) && $_GET['act'] == 'ban' && isSet($_GET['uID'])) {
  2.         $uID = $_GET['uID'];
  3.         $checkUserExists = mysqli_query($con, "SELECT * FROM `users` WHERE `id`='$uID'");
  4.         if (mysqli_num_rows($checkUserExists) > 0) {
  5.                 //User exists, set level to 0 and ban them.
  6.                 $banUserQuery = mysqli_query($con, "UPDATE `users` SET `level`='0' WHERE `id`='$uID'");
  7.                 if ($banUserQuery) {
  8.                         $banMessage = $_POST['message'];
  9.                         $banUserInfo = mysqli_fetch_array($checkUserExists) or die(mysql_error());
  10.                         $banUser = $banUserInfo['username'];
  11.                         $insertReasonQ = mysqli_query($con, "INSERT INTO `reasons` VALUES ('', 'ban', '$banMessage', '$banUser')");
  12.                                 if ($insertReasonQ)
  13.                                         echo 'Banned user.';
  14.                 }else
  15.                         echo 'Failed to ban user...';
  16.                 header("Location:admin.php");
  17.                 exit();
  18.         }
  19. }
Finally we need to output the message to the user once they try to login, but they're banned...
  1. if (isSet($_POST['login']) && isSet($_POST['user']) && isSet($_POST['pass']) && $_POST['user'] != '' && $_POST['pass'] != '') {
  2.         $pass = $_POST['pass'];
  3.         $passMD5 = md5($pass);
  4.         $user = $_POST['user'];
  5.         $q = mysqli_query($con, "SELECT * FROM `users` WHERE `username`='$user'");
  6.         if (mysqli_num_rows($q) > 0) {
  7.                 $info = mysqli_fetch_array($q);
  8.                 if ($info['level'] != '0') {
  9.                         $storedPassword = $info['password'];
  10.                         if ($storedPassword == $passMD5) {
  11.                                 $_SESSION['username'] = $user;
  12.                                 if ($info['level'] == 2)
  13.                                         $_SESSION['isMod'] = 'yes';
  14.                                 if ($info['level'] == 3)
  15.                                         $_SESSION['isAdmin'] = 'yes';
  16.                                 header("Location:forumTutorial.php");
  17.                                 exit();
  18.                                 echo 'Logged in!';
  19.                         }else
  20.                                 echo 'Password was incorrect. Please try again.';
  21.                 }else{
  22.                         $banReasonQ = mysqli_query($con, "SELECT * FROM `reasons` WHERE `additional`='$user' AND `use`='ban'") or die(mysql_error());
  23.                         $banReasonInfo = mysqli_fetch_array($banReasonQ);
  24.                         $banReason = $banReasonInfo['message'];
  25.                         echo 'That account is banned!<br/>Reason: '.$banReason;
  26.                 }
  27.         }else
  28.                 echo 'That username was not found. Please try again.';
  29. }
Message Of The Day: Next we add a message of the day option to the admin panel...
  1. <h1>Message of the Day:</h1>
  2. <form action='admin.php?act=motd' method='POST'>
  3. Current MOTD: <?php
  4.         $motdQ = mysqli_query($con, "SELECT * FROM `reasons` WHERE `use`='motd'");
  5.         if (mysqli_num_rows($motdQ) > 0) {
  6.                 $motdInfo = mysqli_fetch_array($motdQ);
  7.                 echo $motdInfo['message'];
  8.         }
  9. ?>
  10. <br/>
  11. Update MOTD: <input type='text' name='newMOTD' />
  12. </form>
Next we process the information. If there is already an motd, we update it, otherwise we insert a new row to create one...
  1. else if(isSet($_GET['act']) && $_GET['act'] == 'motd') {
  2.         $message = $_POST['newMOTD'];
  3.         $motdQ = mysqli_query($con, "SELECT * FROM `reasons` WHERE `use`='motd'");
  4.         if (mysqli_num_rows($motdQ) > 0) {
  5.                 mysqli_query($con, "UPDATE `reasons` SET `message`='$message' WHERE `use`='motd'");
  6.                 echo 'MOTD Updated.';
  7.         }else{
  8.                 $motdInsert = mysqli_query($con, "INSERT INTO `reasons` VALUES ('', 'motd', '$message', '')");
  9.                 if ($motdInsert) {
  10.                         echo 'Inserted motd.';
  11.                 }else
  12.                         echo 'Failed to insert motd.';
  13.         }
  14. }
Finally we show the motd on the homepage...
  1. <?php
  2.         $motdQ = mysqli_query($con, "SELECT * FROM `reasons` WHERE `use`='motd'");
  3.         $motdInfo = mysqli_fetch_array($motdQ);
  4.         if (mysqli_num_rows($motdQ) > 0 && $motdInfo['message'] != '') {
  5.                 echo '<h1>MOTD:</h1><br/>'.$motdInfo['message'];
  6.         }
  7. ?>

Add new comment