Membuat Form Login dengan PHP & My SQL
Cara membuat form login
Pastikan sudah mengaktifkan XAMPP, Lalu buat folder baru di folder XAMPP -> htdocs dan isikan script dibawah ini :
index.php
<?php
session_start();
include "koneksi.php";
if (isset($_SESSION['username'])==''){
include "login.php";
}else{
include "konten.php";
}
?>
koneksi.php
<?php
$koneksi = mysqli_connect("localhost","root","","siswa_smk");
// Cek koneksi
if (mysqli_connect_errno()){
echo "Koneksi database gagal : " . mysqli_connect_error();
}
?>
konten.php
<?php
echo "Selamat, anda berhasil login
$_SESSION[username]!";
echo "<br>";
echo "<a href=logout.php>Logout</a>";
?>
login.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<h1>Login</h1>
<form action="" method="POST">
<label>Username</label><br>
<input type="text" name="username"><br>
<label>Password</label><br>
<input type="password" name="password"><br>
<button type="submit" name="login">Log in</button>
<p class="login-register-text">Anda belum punya akun? <a href="register.php">Register</a></p>
</form>
<?php
include "koneksi.php";
if (isset($_POST['login'])){
$user = $_POST['username'];
$pass = md5($_POST['password']);
$login=mysqli_query($koneksi, "SELECT * FROM user
WHERE username='$user' AND password='$pass'");
$cocok=mysqli_num_rows($login);
$r=mysqli_fetch_array($login);
if ($cocok > 0){
$_SESSION['username'] = $r['username'];
header('location:index.php');
}else{
echo "<script>window.alert('Maaf, Anda Tidak Memiliki akses');
window.location=('index.php')</script>";
}
}
?>
</body>
</html>
logout.php
<?php
session_start();
session_destroy();
header('Location:index.php');
die();
?>
Setelah itu download dan import file SQL dibawah ini :
Maka tampilannya seperti ini :
Posting Komentar untuk "Membuat Form Login dengan PHP & My SQL"