Saturday, December 16, 2017

Cara Membuat Form Login dengan PHP! Gampang!

Masih di ambangbatas!. Kali ini, kita akan membahas cara membuat form login dengan menggunakan bahasa php. Daripada banyak bacot, kita langsung saja. Yuk simak!
--------------------------------------------------------------------------------------------------------------------------
Contoh :
.

--------------------------------------------------------------------------------------------------------------------------
Buat file baru dengan :
  • cek_login.php
  • daftar.php
  • index.php
  • koneksi.php
  • login.php
  • logout.php
  • proses_daftar.php
cek_login.php
<?php
include "koneksi.php";
$username = $_POST['username'];
$password = $_POST['password'];
if (empty($username)) {
echo "<script>alert('Username kosong')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
} else if (empty($password)) {
echo "<script>alert('Password kosong')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
} else {
session_start();
$login = mysql_query("select * from member where username='$username' and password='$password'");
if (mysql_num_rows($login)>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
<!DOCTYPE html>
<html>
<head>
<title>Daftar</title>
</head>
<body>
<form method="post" name="daftar" action="proses_daftar.php">
Nama : <input type="text" name="nama"><br><br>
Email : <input type="text" name="email"><br><br>
Username : <input type="text" name="username"><br><br>
Password : <input type="password" name="password"><br><br>
<input type="submit" name="submit" value="DAFTAR!"> | Sudah punya akun? <a href="login.php">Login!</a>
</form>
</body>
</html>

index.php
<?php
include "koneksi.php";
session_start();
if (!isset($_SESSION['username'])) {
header("location:login.php");
}
?>
Berhasil login, <a href="logout.php">Logout</a>

koneksi.php
<?php
mysql_connect("localhost","root","") or die ("Gagal Terhubung ke Database");
mysql_select_db("login") or die ("Database Tidak Ditemukan");
?>

login.php
<?php
include "koneksi.php";
session_start();
if (isset($_SESSION['username'])) {
header("location:index.php");
}
?>
<!DOCTYPE html>
<html>
<head>
<title>LOGIN!</title>
</head>
<body>
<form method="post" name="login" action="cek_login.php">
Username : <input type="text" name="username"><br><br>
Password : <input type="password" name="password"><br>
<input type="submit" name="submit" value="LOGIN!"> | Tidak punya akun? <a href="daftar.php">Daftar!</a>
</form>
</body>
</html>

logout.php
<?php
session_start();
session_destroy();

echo "<script>alert('Anda telah Logout!')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
?>

proses_daftar.php
<?php
include "koneksi.php";
$nama = $_POST['nama'];
$email = $_POST['email'];
$username = $_POST['username'];
$password = $_POST['password'];

if (empty($nama)) {
echo "<script>alert('Nama harap diisi!')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
} else if (empty($email)) {
echo "<script>alert('Email harap diisi!')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
} else if (empty($username)) {
echo "<script>alert('Username harap diisi!')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
} else if (empty($password)) {
echo "<script>alert('Password harap diisi!')</script>";
echo "<meta http-equiv='refresh' content='1 url=login.php'>";
} else {
$daftar = mysql_query("INSERT INTO member (username,password,nama,email) values ('$username','$password','$nama','$email')");
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'>";
}
}
?>

Sedangkan untuk database, kita menggunakan MySQL.
Buat database dan tabel, lalu buat kolom dengan :


--------------------------------------------------------------------------------------------------------------------------
Nah, hanya segitu yang dapat kami sampaikan. Jika kurang mengerti, kalian dapat berkomentar dibawah :).


0 comments

Post a Comment