Kategori
PHP

Multi-User Login with PHP and Mysql

Case Study : Creating a Multi-User Login System with PHP and Mysql.

Requirements : Webserver Packages (AppServ, XAMPP, EasyPHP, etc)

Step 1 : Preparing the Database

  1. Prepare a database with the name db_multiuser.
  2. Create a table named tb_user, create the fields – fields like the following picture,

    Struktur Tabel
  3. Insert some sample data to tb_user.
    INSERT INTO `db_multiuser`.`tb_user` (`id`, `username`, `password`, `tipe`) VALUES (NULL, 'ani', 'ani','admin'), (NULL, 'budi', 'budi', 'operator');

Step 2 :Create the configuration file

  1. Type the following script,
    <?php
    $host = "localhost";
    $user = "root"; //adjust to your mysql user

    $pass = ""; //i use no password in my mysql
    $dbName = "db_multiuser";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Connect Failed : ".mysql_error());
    ?>
  2. Save with the name connect.php
  3. Explanation: connect.php is a configuration file that would link the login system that we created with mysql database.

Step 3 :Create the login form

  1. Type the following script,
    <center>
    <h2> <b> Login Page </b> <h2><form method="post" action="proses.php"><table border="0" align="center" cellpadding="5" cellspacing="8"><tr bgcolor="orange">
    <td> UserName! </td>
    <td> <input name="username" type="text"> </td>
    </tr>
    <tr bgcolor="orange">
    <td> Password ! </td>
    <td> <input name="password" type="password"> </td>
    </tr>
    <tr>
    <td> &nbsp; </td>
    <td> <input name="submit" type="submit" value="login"> </td>
    </tr>
    </table>
    </form>
    </center>
  2. Save with the name formlogin.php
  3. This script will display a login form for username and password fields. Consider the action="proses.php". This shows that the entries from the username and password will be processed (given the action) by proses.php file. This file will be created in the next step.

Step 4 :Create the processing file for login

  1. Type the following script,
    <?php
    session_start(); //startsession
    include "connect.php";//get the username and password from the form
    $username = $_POST['username'];
    $password = $_POST['password'];
    //query for retrieve the user data from database according to username in the form
    $q = "SELECT * FROM tb_user WHERE username = '$username' ";
    $result = mysql_query($q);
    $data = mysql_fetch_array($result);
    //checking the password
    if ($password == $data['password']) {
    //save usertype & username in the session
    $_SESSION['tipe'] = $data['tipe'];
    $_SESSION['username'] = $data['username'];
    include "menu.php";
    }
    //If password doesnt match
    else {
    $warning = "Wrong Username / Password";
    echo $warning;
    }
    ?>
  2. Save with the name proses.php

Step 5 : Make a display special for user

  1. Type the following script,
    <?php
    session_start();
    include "connect.php";
    echo "<center>";
    echo "<h3> Menu </h3>";
    if ($_SESSION['tipe'] == "admin") {
    echo " Anda Login sebagai Admin";
    }
    else if ($_SESSION['tipe'] == "user") {
    echo " Anda Login sebagai User";
    }
    ?>
  2. Save with the name menu.php
  3. Explanation: This script will display the information in accordance with the users who are logged.

Step 6 : Running the Login System

  1. Go to http://localhost/
  2. Select file formlogin.php to start running the application. You will see a display like the following,

    Login Page
  3. Set the username = ani; password = ani
  4. System will display the description as follows,

    Admin Login
  5. If you log in as an operator (username: budi; password: budi), it will display information as shown below,

    Operator Login

NB. Save all the php file from step 2 – step 5 into the SAME ONE folder.

Okay then, happy coding (:

Oleh littleflow3r

obviously, a girl. with sweet smile, off course. turning her 21 years trapped on Informatics departement and just started to having some crush with it lately. she uses wordpress. she loves php. she loves to satisfied her shopping hobbies just via looking around in online shop. oh yeah, she got an A for her english subject^^

2 replies on “Multi-User Login with PHP and Mysql”

Tinggalkan Balasan

Alamat email Anda tidak akan dipublikasikan. Ruas yang wajib ditandai *

This site uses Akismet to reduce spam. Learn how your comment data is processed.