PHP/MySQLi Creating a Forum - Part 13 - Users & Registering

PHP/MySQLi Creating a Forum - Part 12 - Listing Messages and Threads Introduction: This tutorial will be continuing my series of creating a forum in PHP/MySQLi/HTML. This part will be covering users. 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. Users Table: To start, we need a users table. Create the table named 'users' with the following information... id - INT - 5 Length - Primary Key - AI username - VARCHAR - 255 Length password - VARCHAR - 255 Length Register: Next lets make a register page, I will call mine 'register.php'. In that we first need a HTML form...
  1. <form action='register.php' method='POST'>
  2.         <table>
  3.                 <tbody>
  4.                         <tr>
  5.                                 <td>Username: </td><td><input type='text' name='user' /></td>
  6.                         </tr>
  7.                         <tr>
  8.                                 <td>Password: </td><td><input type='password' name='pass' /></td>
  9.                         </tr>
  10.                         <tr>
  11.                                 <td></td><td><input type='submit' value='Register' name='reg' /></td>
  12.                         </tr>
  13.                 </tbody>
  14.         </table>
  15. </form>
PHP: To process this, we want to check if the information is valid first. If it is, we want to create an MD5 encrypted hash version of the password, check if the user is already taken and it if it is available, add it as a new row to the table with the encrypted password (MD5 Hash). We also output the outcome...
  1. <?php
  2.         session_start();
  3.         $con = mysqli_connect('localhost', 'root', '', 'forumTutorial');
  4.         if (isSet($_POST['reg']) && isSet($_POST['user']) && isSet($_POST['pass']) && $_POST['user'] != '' && $_POST['pass'] != '') {
  5.                 $pass = $_POST['pass'];
  6.                 $passMD5 = md5($pass);
  7.                 $user = $_POST['user'];
  8.                 $q = mysqli_query($con, "SELECT * FROM `users` WHERE `username`='$user'");
  9.                 if (mysqli_num_rows($q) > 0) {
  10.                         echo 'That username is already taken.';
  11.                 }else{
  12.                         $qq = mysqli_query($con, "INSERT INTO `users` VALUES ('', '$user', '$passMD5')");
  13.                         if ($qq) {
  14.                                 echo 'Registered successfully!';
  15.                         }else
  16.                                 echo 'Failed to register.';
  17.                 }
  18.         }
  19. ?>

Add new comment