Membuat Login pada Website dan Teknik Proses nya dengan PHP MYSQLi

Posted in Membuat Website dari 0 Life at 23 Juli 2024 With 22 Comments

OK... pada SESI 3 kali ini kita akan membuat Halaman Login dengan Menggunakan HTML5 dan CSS3 , Serta Cara Validasi Form Login dengan HTML5  , Kemudian kita akan membuat login dengan data yang di ambil dari database Mysqli dengan PHP

Secara Garis Besar SESI III ini akan terbagi menjadi :

  1. Membuat Form Login dengan HTML dan CSS3 (login.php dan login.css)
  2. Membuat Database di phpmyadmin dengan nama "db_websaya"
  3. Membuat tabel di database dengan nama "users"
  4. Membuat Koneksi ke database dengan PHP 7 (koneksi.php)
  5. Otentikasi atau Verifikasi Pengguna Yang Masuk (aksi_login.php)
  6. Membuat Dashbord atau File (index.php dan style.css) , dan kemudian kita tambahkan script otentikasi user yang di ambil dari SESSION yang login
  7. Membuat Logout.php

Adapun File dalam SESI III ini yaitu  :

├── indeks.php
├── koneksi.php
├── login.php
├── logout.php
├── style.css
├── login.css

1. MEMBUAT FORM LOGIN HTML DAN CSS3

membuat Form Login css3 untuk web dinamis php mysqli

OK.. sekarang kita akan membuat Form Login pada File login.php dimana cssnya bernama login.css , adapun script nya ada dibawah ini :

login.php

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>
<div class="container">
    <h2>Form Login</h2>
  <form action="">
    <label>Username</label>
    <input type="text" name="username" required>

    <label>Password</label>
    <input type="password" name="password" required>
    
    <input type="submit" value="Submit">
  </form>
</div>
</body>
</html>

Proses PHP untuk File login.php ,

<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="login.css">
</head>
<body>
<div class="container">
    <h2>Form Login</h2>
<?php
    require('koneksi.php');
    // If form submitted, insert values into the database.
    if (isset($_POST['submit'])){
        
      $username =$_POST['username'];
      $username = mysqli_real_escape_string($konek,$username);
      $password = $_POST['password'];
      $password = mysqli_real_escape_string($konek,$password);
      $password = md5($password);
      $query = "SELECT * FROM users WHERE username='$username' and password='$password'";
      $tampil = mysqli_query($konek,$query) or die(mysqli_error($konek));
      $rows = mysqli_num_rows($tampil);
        if($rows==1){
          $_SESSION['username'] = $username;
          // Redirect user to index.php
          header("Location: index.php");
          exit();
            }else{
      echo "
        <h3>Username/password Salah.</h3>
        <br/>Click here to <a href='login.php'>Login</a>";
      }
        }else{
?>
  <form action="" method="POST">
    <label>Username</label>
    <input type="text"  name="username" required>

    <label>Password</label>
    <input type="password"  name="password" required>
    
    <input type="submit" value="Submit" name="submit">
  </form>
  <?php } ?>
</div>
</body>
</html>

login.css


body {
    background: linear-gradient(-45deg, #ee7752, #e73c7e, #23a6d5, #23d5ab);
    background-size: 400% 400%;
    animation: gradient 15s ease infinite;
    height: 100vh;
}
h2,label{
    font-family:Arial, Helvetica, sans-serif;
}
/* Style container*/
.container {
  background-color:#fff;
  padding: 20px;
  width:300px;
  margin:0 auto;
  margin-top:100px;
}
.container h2 {
    text-align: center;
}
/* Style input */
input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ccc;
  border-radius: 4px;
  box-sizing: border-box;
  margin-top: 6px;
  margin-bottom: 16px;
}

/* Style tombol submit */
input[type=submit] {
  background-color: #04AA6D;
  color: white;
}

2. MEMBUAT DATABASE

Untuk Membuat database , silahkan aktifkan web server apache dan mysql di xampp , kemudian akses localhost/phpmyadmin, pada bagian TAB database buat database dengan nama "db_websaya" pada field yang disediakan , atau anda bisa menggunakan query dibawah ini :

CREATE DATABASE db_websaya;

3. MEMBUAT TABEL "USERS" DI DATABASE

Name Type
id_users int(11)
username varchar(50)
email varchar(50)
password varchar(50)

Untuk membuat tabel anda bisa mengeksekusi query berikut ini :

CREATE TABLE IF NOT EXISTS `users` (
 `id_users` int(11) NOT NULL AUTO_INCREMENT,
 `username` varchar(50) NOT NULL,
 `email` varchar(50) NOT NULL,
 `password` varchar(50) NOT NULL,
 PRIMARY KEY (`id_users`)
 );

INSERT data di tabel USERS

Disini kita akan menambahkan data pada tabel users , yang sudah kita buat berupa nama user dan password

INSERT INTO `db_websaya`.`users` (`id_users`, `username`, `email`, `password`) VALUES ('1', 'admin', 'admin@gmail.com', MD5('admin'));

4. MEMBUAT koneksi.php

koneksi.php merupakan script PHP yang kita gunakan untuk melakukan koneksi dengan database db_websaya yang kita buat di database webserver xampp phpmyadmin

<?php
$konek = mysqli_connect("localhost","root","","db_websaya");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
?>

5. MEMBUAT index.php dan style.css

File ini kita ambil dari SESI II , dan pada bagian atas sekali , kita tambahkan script berikut ini yang merupakan script otentikasi Pengecekan Apakah User sudah Login sesuai dengan  SESSION ,

<?php
session_start();
if(!isset($_SESSION["username"])){
header("Location: login.php");
exit(); }
?>

6. MEMBUAT logout.php

<?php
session_start();
// Destroying All Sessions
if(session_destroy())
{
// Redirecting ke Halaman Login
header("Location: login.php");
exit();
}
?>

Diskusi

22 Komentar


Lidia   Pada : 2024-08-18 13:19:24
https://alfarep.com/plastic-surgeon/In the competitive landscape of today’s digital world, local businesses need to focus on their online reputation to stand out and thrive. A positive online reputation can drive more customers to your busin

Philipp   Pada : 2024-09-10 00:24:32
https://watchnow.gomuviz.com/ Hi friends, its impressive piece of writing concerning teachingand fully defined, keep it up all the time.

Tyree   Pada : 2024-09-14 07:20:23
https://magicboxpro.flowcartz.com/ Your mode of telling all in this article is actually good, all be capable of effortlessly be aware of it, Thanks a lot.

hledam pujcku   Pada : 2024-10-02 13:46:20
Přijetí hypoteční platby může být obtížné pokud nemáte rádi čekání v dlouhých řadách , vyplnění intenzivní formuláře , a odmítnutí úvěru na základě vašeho úvěrového skóre . Přijímání hypoteční platby může

hledam pujcku   Pada : 2024-10-02 13:46:42
Přijetí hypoteční platby může být obtížné pokud nemáte rádi čekání v dlouhých řadách , vyplnění intenzivní formuláře , a odmítnutí úvěru na základě vašeho úvěrového skóre . Přijímání hypoteční platby můž

Antwan   Pada : 2024-10-13 03:15:30
https://dentavim.colibrim.ca Great post. I used to be checking continuously this weblog and I am inspired! Extremely helpful information specifically the ultimate phase : ) I maintain such information much. I was looking for this certain info f

Chiquita   Pada : 2024-12-09 10:40:14
Remarkable post, thanks for sharing for providing. - https://agile-coach.s3.us-east-005.backblazeb2.com/agile-coach-responsibilities-and-essential-qualities.html

Vallie   Pada : 2024-12-18 19:00:41
https://lxgonline.mn.co/members/30482610 Rank and Rent is an innovative appproach to online business that leveages search envine optimization (SEO) to gnerate recurring revenue. The concept revolves around creating a website, optimizing it for

Prince   Pada : 2024-12-22 02:38:56
https://turfshowtimes.com/users/Rank_And_Rent_Plugin Rank and Rent is a powerful digital marketing strategy that utilizes SEO too create a sustainable online incomne stream. The idea is to rank a website and rent it to local businesses seeing new

Benjamin   Pada : 2024-12-23 22:57:37
Wonderful article, thank you for offering. - https://agile-foundation.s3.us-east-005.backblazeb2.com/introduction-to-agile-foundation-basics-and-benefits.html

Windy   Pada : 2025-01-03 03:44:22
cheating wife confessions quora reviews of wicked

Susie   Pada : 2025-01-04 06:14:45
Excellent insight, grateful for the insights. - https://agile-coach.s3.us-east-005.backblazeb2.com/agile-coach-responsibilities-and-essential-qualities.html

Clarence   Pada : 2025-01-05 05:09:27
Awesome blog, thank you for sharing. - https://agile-design.s3.ca-central-1.wasabisys.com/importance-of-agile-requirements-gathering-in-design.html

Odette   Pada : 2025-01-11 04:38:55
Impressive post, grateful for sharing. - https://agile-coach.s3.us-east-005.backblazeb2.com/agile-coach-responsibilities-and-essential-qualities.html

Hassie   Pada : 2025-02-01 06:59:39
Amazing write-up, cheers for writing. - https://www.nba.com/

Hazel   Pada : 2025-02-13 13:51:04
Amazing article, appreciate it for the information. -http://local.standard.co.uk/company/de7e42fcee11732e63010dce4a581176

Dacia   Pada : 2025-02-14 18:06:02
Impressive write-up, cheers for writing. -https://www.uxbridgepages.co.uk/company/02b16bb2c47cf2655d7e96d337c2d6bb

Madelaine   Pada : 2025-02-15 08:53:49
Great piece, thank you for providing. -https://gb.centralindex.com/company/ad7768016a9ee8c4a5ca1ec0a36f5576/knowledge-train-birmingham/birmingham

Nicole   Pada : 2025-02-28 09:06:03
Wonderful content, thanks for sharing for putting this together. -http://www.westminsterpages.co.uk/company/de34b836282fb6b97df71708b3c0ea77

Claudio   Pada : 2025-03-03 16:30:09
Excellent blog, many thanks for this knowledge. -https://directory.leicestermercury.co.uk/company/dd85e74c7dbd3e87b949e2e66194120b

Ruth   Pada : 2025-03-08 05:17:32
adana halı yıkama sevgi halı yıkama adana Unquestionably believe that which you said. Your favorite justification appeared to be on the internet the easiest thing to be aware of. I say to you, I definitely get irked while people think abou

Roxanne   Pada : 2025-03-09 05:46:55
Great read, many thanks for posting. -http://www.southwarkpages.co.uk/company/e29a9d3f770acb8e079252da23aff4ff



wa