Membuat Sistem Login PHP Mysql Sederhana

Belajar PHP Mysql Sistem Login dengan Session

Cara membuat sistem login, logout, register user sederhana menggunakan php dan database mysql di localhost xampp.

Panduan belajar php ini menjadi dasar pengenalan php mysql dengan fitur lanjutan didalam sistem login mengenal function php session, untuk mengamankan user login dengan key password dan username.

Buat database Mysql baru

Nama database : login

CREATE TABLE `user` (
`id` int(5) NOT NULL,
`nama` varchar(50) NOT NULL,
`username` varchar(25) NOT NULL,
`password` varchar(120) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

INSERT INTO `user` (`id`, `nama`, `username`, `password`) VALUES
(12, 'Andreas', 'seo', '123');

ALTER TABLE `user`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `username` (`username`);

ALTER TABLE `user`
MODIFY `id` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=16;

Buat folder latihan php di xampp

Create new folder : login

READ :  Iklan CPM POP-Under vs Interstitial, Bagus Mana?

File :

  • connect.php
  • index.php
  • login.php
  • register.php
  • dashboard.php
  • function.php

 

Isi script file

connect.php

<?php

$user='seo';
$pass='123';
$host='localhost';
$db='login';

$db=mysqli_connect($host,$user,$pass,$db);
if(!$db){
echo "Connected DB Failed!";
}?>

index.php

<?php
include'login.php';

login.php

<?php 
session_start();
if($_SESSION['login']==1){header('location:dashboard.php');}?>

<div style="padding: 35px; background: #eee;">
<h2>Sig In user</h2>
<form action="function.php?utm=login" method="post">

<p>Username
<input type="text" name="username"></p>
<p>Password
<input type="Password" name="password"></p>
<p><button type="sumbit">Login</button> &nbsp;&nbsp; Create: <a href="register.php">New user</a></p>
</form>
</div>

register.php

<div style="padding: 35px; background: #eee;">
<h2>Add user</h2>
<form action="function.php?utm=register" method="post">
<p>Nama
<input type="text" name="nama"></p>
<p>Username
<input type="text" name="username"></p>
<p>Password
<input type="Password" name="password"></p>
<p><button type="sumbit">Register</button></p>
</form>
</div>

dashboard.php

<?php
session_start();
if(!$_SESSION['login']==1){header('location:login.php');} else {?>

<div style="padding: 35px; background: #eee;">
<h2>Dashboard</h2>
<p>Ini adalah halaman dashboard user '<b><?php echo $_SESSION['user'];?></b>' - <a href="function.php?utm=logout">Logout</a></p>
</div>

<?php } ?>

function.php

<?php
session_start();
include'connect.php';

$nama=$_POST['nama'];
$user=$_POST['username'];
$pass=$_POST['password'];

if($_GET['utm']=='register'){
$q="insert into user (nama,username,password) 

    values ('$nama','$user','$pass')";

$reg=$db->query($q);

if($reg){echo "Register succesfuly!, <a href='login.php'>Login now!</a>";} else {echo"Register failed!";}
}
if($_GET['utm']=='login'){
$q="select * from user where username='$user' and password='$pass'";
$log=$db->query($q);
$cek=$log->num_rows;

if($cek==1){
$_SESSION['user']=$user;
$_SESSION['login']=1;
header('location:dashboard.php');}else{echo "login failed!";}
}
if($_GET['utm']=='logout'){
session_destroy();
header('location:login.php');
}

SS Tampilan login PHP Mysql

Pengembangan Sistem Login

karena ini masih sederhana perlu ditambahkan, kembangkan:

  1. Desain yang lebih bagus
  2. Validasi form
  3. Password Hash
  4. Lupa password
  5. Limit Login, dll.
READ :  Review Iconic One Pro Tema Wordpress SEO + Cepat

File Download

Unduh file sistem login php secara gratis dan silahkan dikembangkan: login.zip

Update : 26 Feb, 2024