PHP/MySQLi Creating a Forum - Part 14 - User Login and User Levels Theory

PHP/MySQLi Creating a Forum - Part 13 - Users & Registering Introduction: This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. This part will be covering logging in for users and running over the theory of user levels. Pre-creation: First you will need a host for your PHP, either a web host or localhost is fine but you will need PHP and MySQL(i) capabilities. Also, this will not be covering creating users, or styling the pages. For the purpose of using the logged in users username, we will be using $_SESSION['username']; from my login script, you can find that tutorial on my profile page. Obviously you will also need to go through all the previous parts of this tutorial series which can all be found on my profile tracking page. login.php: First we will create the HTML form for the users to login to their account through, I am going to do this on a new login.php page...
  1. </head>
  2. <form action='login.php' method='POST'>
  3.         <table>
  4.                 <tbody>
  5.                         <tr>
  6.                                 <td>Username: </td>
  7.                                 <td><input type='text' name='user' /></td>
  8.                         </tr>
  9.                         <tr>
  10.                                 <td>Password: </td>
  11.                                 <td><input type='password' name='pass' /></td>
  12.                         </tr>
  13.                         <tr>
  14.                                 <td></td>
  15.                                 <td><input type='submit' value='Log in' name='login' /></td>
  16.                         </tr>
  17.                 </tbody>
  18.         </table>
  19. </form>
  20. </body>
  21. </html>
Again, just like the register form; this will need a username and password to be entered by the user. The action of the form goes back to itself on login.php so that is where we are going to place our PHP processing code (at the top of the document). Login: To process the login information, we first want to check all the information is there and validate it. If it is all ok, we will stored the entered information in our local user and pass variables and then encrypt the password to MD5 (Since it is encrypted within our database and we need to compare the two strings later on) adn store the encrypted password in our passMD5 variable. Next, we check if the username exists, and if it does we get the password in that row of the users table for the user account (md5 encrypted already). Finally we compare the two password strings and send the user to forumTutorial.php if they have logged in successfully. We also output any necessary information.
  1. <?php
  2. $con = mysqli_connect('localhost', 'root', '', 'forumTutorial');
  3. if (isSet($_POST['login']) && isSet($_POST['user']) && isSet($_POST['pass']) && $_POST['user'] != '' && $_POST['pass'] != '') {
  4.         $pass = $_POST['pass'];
  5.         $passMD5 = md5($pass);
  6.         $user = $_POST['user'];
  7.         $q = mysqli_query($con, "SELECT * FROM `users` WHERE `username`='$user'");
  8.         if (mysqli_num_rows($q) > 0) {
  9.                 $info = mysqli_fetch_array($q);
  10.                 $storedPassword = $info['password'];
  11.                 if ($storedPassword == $passMD5) {
  12.                         $_SESSION['username'] = $user;
  13.                         header("Location:forumTutorial.php");
  14.                         exit();
  15.                         echo 'Logged in!';
  16.                 }else
  17.                         echo 'Password was incorrect. Please try again.';
  18.         }else
  19.                 echo 'That username was not found. Please try again.';
  20. }
  21. ?>
User Levels Theory: Before I begin on the theory of User Levels, I am not currently planning on implementing this in to our forum, however if it is requested by enough people, I will do so (if you want the tutorial for the user levels, let me know through Private Message or by replying to this thread). So, what are User Levels? A user level is a way of defining how important a single user account is. The levels can be in any data type (string, integer, double, boolean, etc.), for example; (Admin, Moderator, Uploader), (1, 2, 3), or (true, false) - true or false could just be used to determine who can edit threads. User levels are fairly important for a full forum because without it, no one would be able to moderate the forum. However, depending on how detailed the user levels are, it could be a long sub-series of this series of forum creation.

Comments

hey, when i enter the correct username and password from my database i still get incorrect password any ideas?

Add new comment