Tutorial Membuat Sistem Login Register Mudah dengan PHP MySQLi
Pada tutorial kali ini akan membahas cara membuat sistem login register yang simpel (sehingga form-form yang dibuat biasa saja). Namun tetap mempertahankan konsep Login Register menggunakan PHP MySQLi. Oke, langsung saja kita mulai.
Buat database dengan nama sistem_login_simpel pada phpMyAdmin. Kemudian buat tabel dengan nama admin di dalam database tersebut, dengan atribut sesuai gambar di bawah ini.
Buat folder dalam htdocs (pengguna XAMPP) dengan nama sistem_login_simpel dengan struktur file sesuai gambar di bawah ini.
Buat database dengan nama sistem_login_simpel pada phpMyAdmin. Kemudian buat tabel dengan nama admin di dalam database tersebut, dengan atribut sesuai gambar di bawah ini.
Buat folder dalam htdocs (pengguna XAMPP) dengan nama sistem_login_simpel dengan struktur file sesuai gambar di bawah ini.
Lalu buat file-file yang dibutuhkan
config.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$server = "localhost";//host phpmyadmin | |
$user = "root";//username phpmyadmin (default : root) | |
$pass = "";//password phpmyadmin | |
$db = "sistem_login_simpel";//nama database | |
//menghubungkan aplikasi web dengan database kita | |
$connect = mysqli_connect($server, $user, $pass, $db); | |
?> |
login.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "config.php"; | |
//fungsi untuk memulai sesi yang menyimpan informasi login | |
session_start(); | |
//apabila user sudah login, redirect ke index.php | |
if(isset($_SESSION['user'])){ | |
header("Location: index.php"); | |
} | |
//apabila form login udah disubmit | |
if($_SERVER['REQUEST_METHOD'] == 'POST'){ | |
$username = $_POST['username']; | |
$password = md5($_POST['password']); | |
$result = mysqli_query($connect, "SELECT * FROM admin WHERE username='$username' AND password='$password'"); | |
$row = mysqli_fetch_assoc($result); | |
if(mysqli_num_rows($result) == 1){ | |
$_SESSION['user'] = $row['id']; | |
header("Location: index.php"); | |
}else{ | |
echo "User belum terdaftar"; | |
} | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Login</title> | |
</head> | |
<body> | |
<form method="post" action=""> | |
<input type="text" name="username" placeholder="Username"><br><br> | |
<input type="password" name="password" placeholder="Password"><br><br> | |
<input type="submit" value="Login"> | |
</form> | |
</body> | |
</html> |
index.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
require_once "config.php"; | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Sistem Login Simpel</title> | |
</head> | |
<body> | |
<h1>Sistem Login Simpel</h1> | |
<?php if(!isset($_SESSION['user'])){ ?> | |
<a href="login.php">Login</a> | <a href="register.php">Register</a><br> | |
<?php }else{ ?> | |
<a href="edit_profile.php">Edit Profile</a> | | |
<a href="logout.php">Logout</a><br> | |
<?php } ?> | |
</body> | |
</html> |
register.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once "config.php"; | |
if($_SERVER['REQUEST_METHOD'] == 'POST'){ | |
$username = $_POST['username']; | |
$password = md5($_POST['password']); | |
mysqli_query($connect, "INSERT INTO admin (username, password) VALUES ('$username', '$password')"); | |
header("Location: index.php"); | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Register User</title> | |
</head> | |
<body> | |
<form method="post" action=""> | |
<input type="text" name="username" placeholder="Username"><br><br> | |
<input type="password" name="password" placeholder="Password"><br><br> | |
<input type="submit" value="Add User"> | |
</form> | |
</body> | |
</html> |
edit_profile.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
if(!isset($_SESSION['user'])){ | |
header("Location: login.php"); | |
} | |
require_once "config.php"; | |
$id = $_SESSION['user']; | |
$admin = mysqli_query($connect, "SELECT * FROM admin WHERE id='$id'"); | |
$row_admin = mysqli_fetch_assoc($admin); | |
if($_SERVER['REQUEST_METHOD'] == 'POST'){ | |
$username = $_POST['username']; | |
$password = md5($_POST['password']); | |
mysqli_query($connect, "UPDATE admin SET username='$username', password='$password' WHERE id='$id'"); | |
header("Location: index.php"); | |
} | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>Update Profile</title> | |
</head> | |
<body> | |
<form method="post" action=""> | |
<input type="text" name="username" placeholder="Username" value="<?php echo $row_admin['username']; ?>"><br><br> | |
<input type="password" name="password" placeholder="password"><br><br> | |
<input type="submit" value="Update"> | |
</form> | |
</body> | |
</html> |
logout.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
session_start(); | |
session_unset($_SESSION['user']); | |
header("Location: login.php"); | |
?> |
Berakhir sudah tutorial membuat sistem login register dengan menggunakan PHP MySQLi. Sampai jumpa di tutorial berikutnya!
Komentar
Posting Komentar