semangat pagi,,
postingan kali ini akan melanjutkan pembahasan sebelumnya yaitu Tutorial Koneksi PHP ke Database Mysql
Nah, karena pada postingan sebelumnya sudah melakukan koneksi php ke Database Mysql maka sekarang kita akan membuat Login sederhana dengan Php dan Mysql.
okeh, mari ikuti langkah-langkah berikut ini :
1. Pastikan Xampp telah Start.
Gambar 1.1 Start Xampp
2. Membuka web browser dengan alamat “localhost/dashboard” dan membuka PypMyAdmin
Gambar 1.2 Tampilan Localhost/dashboard
Gambar 1.3 Insert tabel user
Gambar 1.4 Tampilan Tabel user
Gambar 1.5 Folder admin
# login.php
<?php
session_start();
require_once('../conn/conn.php');
if(isset($_POST['login'])){
$unm = $_POST['user'];
$ups = $_POST['pass'];
$sql = "SELECT * FROM user WHERE username='".$unm."' AND password='".$ups."'";
$result = $conn->query($sql);
//success
if($result->num_rows > 0){
$_SESSION['user'] = $unm;
header('location: index.php');
}else{echo "GAGAL LOGIN";}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Praktikum</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../asset/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-6 col-lg-4 col-lg-offset-4">
<div class="account-wall">
<h1 class="text-center login-title">HALAMAN ADMIN</h1>
<form class="form-signin" method="post">
<input type="text" name="user" class="form-control" placeholder="userame" required autofocus>
<input type="password" name="pass" class="form-control" placeholder="password" required>
<input type="submit" name="login" class="btn btn-primary btn-block" value="Masuk">
</form>
</div>
</div>
</div>
</div>
<script src="../asset/js/jquery.min.js"></script>
<script src="../asset/js/bootstrap.min.js"></script>
</body>
</html>
session_start();
require_once('../conn/conn.php');
if(isset($_POST['login'])){
$unm = $_POST['user'];
$ups = $_POST['pass'];
$sql = "SELECT * FROM user WHERE username='".$unm."' AND password='".$ups."'";
$result = $conn->query($sql);
//success
if($result->num_rows > 0){
$_SESSION['user'] = $unm;
header('location: index.php');
}else{echo "GAGAL LOGIN";}
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Praktikum</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="../asset/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-sm-6 col-lg-4 col-lg-offset-4">
<div class="account-wall">
<h1 class="text-center login-title">HALAMAN ADMIN</h1>
<form class="form-signin" method="post">
<input type="text" name="user" class="form-control" placeholder="userame" required autofocus>
<input type="password" name="pass" class="form-control" placeholder="password" required>
<input type="submit" name="login" class="btn btn-primary btn-block" value="Masuk">
</form>
</div>
</div>
</div>
</div>
<script src="../asset/js/jquery.min.js"></script>
<script src="../asset/js/bootstrap.min.js"></script>
</body>
</html>
# logout.php
<?php
session_start();
//menghapus sessions
if(session_destroy()){
//langsung mengarah ke home index.php
header("location: ../index.php")
}
?>
session_start();
//menghapus sessions
if(session_destroy()){
//langsung mengarah ke home index.php
header("location: ../index.php")
}
?>
# index.php
<?php
session_start(); // memulai session
if(empty($_SESSION['user'])){
echo '
<script language="javascript">
document.location="login.php";
</script>';
}else{
echo 'SELAMAT DATANG : ' .$_SESSION['user'];
}
?>
session_start(); // memulai session
if(empty($_SESSION['user'])){
echo '
<script language="javascript">
document.location="login.php";
</script>';
}else{
echo 'SELAMAT DATANG : ' .$_SESSION['user'];
}
?>
nb: pastikan untuk penamaan file menggunakan huruf kecil saja
6. Selanjutnya buka akses Halaman Admn di browser "localhost/praktikum/admin"
Gambar 1.6 Halaman Admin
Gambar 1.7 Tampilan login berhasil
Sekian Tutorial membuat Login sederhana dengan menggunakan Php dan Mysql.
Semoga bermanfaat.
Assalamualaikum Wr. WB
0 komentar:
Posting Komentar