Membuat Form login dengan php MySQLi

Tutorial Cara Membuat Login Dengan PHP MySQLi - Dalam pembuatan website tentunya memerlukan sebuah form, baik untuk menentukan member atau admin.

  1. Buat Database
  2. CREATE TABLE IF NOT EXISTS `users` (   `id_user` int(11) NOT NULL auto_increment,   `nama` varchar(50) NOT NULL,   `username` varchar(50) NOT NULL,   `password` varchar(50) NOT NULL,   PRIMARY KEY  (`id_user`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

  3. File PHP 
    • koneksi.php
    • <?php
      $servername = "localhost";
      $username = "root";
      $password = "";
      $dbname = "cobaphp"; //pilih database
      // Membuat koneksi
      $conn = mysqli_connect($servername, $username, $password, $dbname);

      ?>


  • index.php

  • <!-- menghubungkan database dan menyatakan jika tidak login maka akan diarahkan ke halaman login -->
    <?php
    include "koneksi.php";
    session_start();
    if (!isset($_SESSION['username'])){
    header ("location:login.php");
    }
    ?>
    <!-- tampilan ketika sukses login -->
    <!doctype html>
    <html>
    <head>
    <title>GheavShare.com</title>
    </head>
    <body>
    <div class="container" align="center">
    <h1>Berhasil Login </h1>
    <br>
    <a href="logout.php">Logout</a>
    </div>
    </body>
    </html>


  • login.php

  • <?php
    include "koneksi.php";
    session_start();
    if (isset($_SESSION['username'])){
    header ("location:index.php");
    }
    ?>
    <html>
    <head>
    <title>LOGIN</title>
      <meta charset="utf-8">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    </head>
    <body>
    <div class="container"  >
    <div style="margin: 10% 0 0 20%;">
    <form class="form-horizontal" method="post" name="login" action="ceklogin.php">

     <div class="form-group">
          <label class="control-label col-sm-2" for="username">Username:</label>
          <div class="col-sm-4">
            <input type="text" class="form-control" id="username" placeholder="Enter username" name="username">
          </div>
        </div>
        <div class="form-group">
          <label class="control-label col-sm-2" for="password">Password:</label>
          <div class="col-sm-4">          
            <input type="password" class="form-control"  placeholder="Enter password" name="password">
          </div>
        </div>
        <div class="form-group" >        
          <div class="col-sm-offset-3 col-sm-4">
            <button type="submit" class="btn btn-primary" value="login">Login</button>
    <a href="daftar.php"><span class="glyphicon glyphicon-user"></span> Sign Up</a>
          </div>
        </div>
    </form>
    </div>
    </div>
    </body>
    </html>




  • ceklogin.php

  • <?php
    include "koneksi.php";
    $username = $_POST['username'];
    $password = $_POST['password'];
    if (empty($username)){
    echo "<script>alert('Username belum diisi')</script>";
    echo "<meta http-equiv='refresh' content='1 url=login.php'>";
    }else if (empty($password)){
    echo "<script>alert('Password belum diisi')</script>";
    echo "<meta http-equiv='refresh' content='1 url=login.php'>";
    }else{
    session_start();
    $login = "select * from users where username='$username' and password='$password'";
    $result = mysqli_query($conn,$login);
    if (mysqli_num_rows($result) > 0){
    $_SESSION['username'] = $username;
    header("location:index.php");
    }else{
    echo "<script>alert('Username atau Password salah')</script>";
    echo "<meta http-equiv='refresh' content='1 url=login.php'>";
    }
    }
    ?>



  • daftar.php

  • <html> <head> <title>GheavShare.com</title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> </head> <body> <div class="container" > <div style="margin: 10% 0 0 20%;"> <form class="form-horizontal" method="post" name="login" action="prosesdaftar.php"> <div class="form-group"> <label class="control-label col-sm-2" for="name">Name:</label> <div class="col-sm-4"> <input type="text" class="form-control" id="name" placeholder="Enter your name" name="nama"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="email">Email:</label> <div class="col-sm-4"> <input type="email" class="form-control" id="email" placeholder="Enter email " name="email"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="username">Username:</label> <div class="col-sm-4"> <input type="text" class="form-control" id="username" placeholder="Enter username" name="username"> </div> </div> <div class="form-group"> <label class="control-label col-sm-2" for="password">Password:</label> <div class="col-sm-4"> <input type="password" class="form-control" placeholder="Enter password" name="password"> </div> </div> <div class="form-group" > <div class="col-sm-offset-3 col-sm-4"> <button type="submit" class="btn btn-primary" value="login">Sign Up</button> <a href="login.php"><span class="glyphicon glyphicon-log-in"></span> Login</a> </div> </div> </form> </div> </div> </body> </html>




  • prosesdaftar.php

  • <?php
    include "koneksi.php";
    $nama = $_POST['nama'];
    $email = $_POST['email'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    if (empty($nama)){
    echo "<script>alert('Nama belum diisi')</script>";
    echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
    }else
    if (empty($email)){
    echo "<script>alert('Email belum diisi')</script>";
    echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
    }else 
    if(empty($username)){
    echo "<script>alert('Username belum diisi')</script>";
    echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
    }else 
    if (empty($password)){
    echo "<script>alert('Password belum diisi')</script>";
    echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
    }else{
    $daftar = "INSERT INTO users (id_user,nama,username,password) values ('$nama','$email','$username','$password')";

    $result = mysqli_query($conn,$daftar);
    if ($daftar){
    echo "<script>alert('Berhasil Mendaftar')</script>";
    echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
    }else{
    echo "<script>alert('Gagal Mendaftar')</script>";
    echo "<meta http-equiv='refresh' content='1 url=daftar.php'>";
    }
    }
    ?>


  • logout.php

  • <?php
    session_start();
    session_destroy();
    echo "<script>alert('Terima kasih, Anda Berhasil Logout')</script>";
    echo "<meta http-equiv='refresh' content='1 url=login.php'>";
    ?>







    Post a Comment

    0 Comments