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 (:

Kategori
PHP

Login Multi-User dengan PHP dan Mysql

Studi Kasus : Membuat Sistem Login Multi-User dengan PHP dan Mysql.
Kebutuhan : Paket Webserver (AppServ, XAMPP, EasyPHP, dll)

Step 1 : Menyiapkan Database

  1. Siapkan database dengan nama db_multiuser.
  2. Buat tabel dengan nama tb_user, buat dengan field – field seperti gambar berikut,

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

Step 2 :Membuat file konfigurasi

  1. Ketikkan script berikut,
    <?php
    $host = "localhost";
    $user = "root"; //sesuaikan dengan user mysql anda
    $pass = ""; //sesuaikan dengan password mysql anda
    $dbName = "db_multiuser";
    mysql_connect($host, $user, $pass);
    mysql_select_db($dbName)
    or die ("Koneksi Gagal : ".mysql_error());
    ?>
  2. Simpan dengan nama connect.php
  3. Penjelasan : connect.php merupakan file konfigurasi yang akan menghubungkan sistem login yang kita buat dengan mysql database.

Step 3 :Membuat form login

  1. Ketikkan script berikut,
    [sourcecode]&lt;center&gt;
    &lt;h2&gt; &lt;b&gt; Login Page &lt;/b&gt; &lt;h2&gt;&lt;form method="post" action="proses.php"&gt;&lt;table border="0" align="center" cellpadding="5" cellspacing="8"&gt;&lt;tr bgcolor="orange"&gt;
    &lt;td&gt; UserName Anda ! &lt;/td&gt;
    &lt;td&gt; &lt;input name="username" type="text"&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr bgcolor="orange"&gt;
    &lt;td&gt; Password ! &lt;/td&gt;
    &lt;td&gt; &lt;input name="password" type="password"&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
    &lt;td&gt; &amp;nbsp; &lt;/td&gt;
    &lt;td&gt; &lt;input name="submit" type="submit" value="login"&gt; &lt;/td&gt;
    &lt;/tr&gt;
    &lt;/table&gt;
    &lt;/form&gt;
    &lt;/center&gt; [/sourcecode]
  2. Simpan dengan nama formlogin.php
  3. Script ini akan menampilkan form login untuk isian username dan password. Perhatikan bagian action="proses.php". Ini menunjukkan bahwa isian dari username dan password akan diproses (diberi action) oleh file proses.php. File ini akan kita buat pada step berikutnya.

Step 4 :Membuat pemrosesan login

  1. Ketikkan script berikut,
    <?php
    session_start(); //memulai session
    include "connect.php";//mengambil isian username dan password dari form
    $username = $_POST['username'];
    $password = $_POST['password'];
    //query untuk mengambil data user dari database sesuai dengan username inputan form
    $q = "SELECT * FROM tb_user WHERE username = '$username' ";
    $result = mysql_query($q);
    $data = mysql_fetch_array($result);
    //cek kesesuaian password masukan dengan database
    if ($password == $data['password']) {
    //menyimpan tipe user dan username dalam session
    $_SESSION['tipe'] = $data['tipe'];
    $_SESSION['username'] = $data['username'];
    include "menu.php";
    }
    //jika password tidak sesuai
    else {
    $warning = "Username / Password Salah";
    echo $warning;
    }
    ?>
  2. Simpan dengan nama proses.php

Step 5 : Membuat tampilan khusus user

  1. Ketikkan script berikut,
    <?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. Simpan dengan nama menu.php
  3. Penjelasan : Script ini akan menampilkan informasi sesuai dengan siapa user yang login.

Step 6 : Menjalankan Sistem Login

  1. Pergi ke http://localhost/
  2. Pilih file formlogin.php untuk mulai menjalankan aplikasi. Anda akan melihat tampilan seperti berikut,

    Login Page
  3. Masukkan username = ani; password = ani
  4. Maka sistem akan menampilkan keterangan seperti berikut,

    Admin Login
  5. Jika anda login sebagai operator (username : budi; password : budi), maka akan muncul informasi seperti gambar dibawah,

    Operator Login

NB. Simpan semua file php dari step 2 – step 5 ke dalam SATU folder yang SAMA.

Okay, happy coding (: