Buatlah file seperti gambar di bawah ini
koneksi.php
<?php
$host = "localhost";
$username = "root";
$password ="";
$database = "login";
$connection = mysql_connect($host,$username,$password) or die("no database :
".mysql_error());
$database = mysql_select_db($database);
?>
$host = "localhost";
$username = "root";
$password ="";
$database = "login";
$connection = mysql_connect($host,$username,$password) or die("no database :
".mysql_error());
$database = mysql_select_db($database);
?>
form.css
.input {
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.button {
width: 20%;
background-color: #4CAF50;
color: white;
padding: 10px 0px;
margin: 8px 0;
border: none;
border-radius: 1px;
cursor: pointer;
font-size: 15px;
}
.button:hover {
background-color: #45a049;
}
div {
border-radius: 5px;
background-color: #f2f2f2; padding: 20px;
}
.center {
margin: auto;
width: 65%;
border: 10px;
padding: 40px;
}
/* solid #73AD21; */
.centers {
margin: auto;
width: 65%;
}
width: 100%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
.button {
width: 20%;
background-color: #4CAF50;
color: white;
padding: 10px 0px;
margin: 8px 0;
border: none;
border-radius: 1px;
cursor: pointer;
font-size: 15px;
}
.button:hover {
background-color: #45a049;
}
div {
border-radius: 5px;
background-color: #f2f2f2; padding: 20px;
}
.center {
margin: auto;
width: 65%;
border: 10px;
padding: 40px;
}
/* solid #73AD21; */
.centers {
margin: auto;
width: 65%;
}
login.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title> contoh login </title>
<body>
<h2 class="centers"> <center> Contoh Login Form </center> </h2>
<br>
<div class="center">
<form action="proseslogin.php" method="post">
<label>Username : </label>
<input type="text" name="username" required="required" class="input">
<label>Password : </label>
<input type="password" name="password" required="required" class="input">
<input type="submit" value="login" class="button">
<input type="reset" value="reset" class="button">
<input type="button" onclick="location.href='registrasi.php';" class="button"
value="register">
</form>
</body>
</script>
</head>
</html>
<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title> contoh login </title>
<body>
<h2 class="centers"> <center> Contoh Login Form </center> </h2>
<br>
<div class="center">
<form action="proseslogin.php" method="post">
<label>Username : </label>
<input type="text" name="username" required="required" class="input">
<label>Password : </label>
<input type="password" name="password" required="required" class="input">
<input type="submit" value="login" class="button">
<input type="reset" value="reset" class="button">
<input type="button" onclick="location.href='registrasi.php';" class="button"
value="register">
</form>
</body>
</script>
</head>
</html>
proseslogin.php
<?php
session_start();
include "koneksi.php";
$username = $_POST['username'];
$password = $_POST['password'];
$query = "select * from user where username='$username' and password='$password'";
$result = mysql_query($query,$connection);
if(mysql_num_rows($result) >=1)
{
$row = mysql_fetch_array($result);
$_SESSION['username'] = $row['username'];
?>
<meta http-equiv='refresh' content ='0; url=index.php'>
<?php
}
else
{
echo "Username atau Password salah";
?>
<meta http-equiv='refresh' content ='1; url=login.php'>
<?php
}
?>
session_start();
include "koneksi.php";
$username = $_POST['username'];
$password = $_POST['password'];
$query = "select * from user where username='$username' and password='$password'";
$result = mysql_query($query,$connection);
if(mysql_num_rows($result) >=1)
{
$row = mysql_fetch_array($result);
$_SESSION['username'] = $row['username'];
?>
<meta http-equiv='refresh' content ='0; url=index.php'>
<?php
}
else
{
echo "Username atau Password salah";
?>
<meta http-equiv='refresh' content ='1; url=login.php'>
<?php
}
?>
index.php
<?php
session_start();
include "login_session.php";
include "koneksi.php";
?>
<html>
<head>
<title> Login Berhasil </title>
<link rel="stylesheet" type="text/css" href="form.css">
<body>
<br>
<br>
<center>
Selamat Datang
<?php echo $_SESSION['username']; ?>
<br>
Terima kasih sudah login
<br>
<button onclick="location.href='login_out.php';" class="button"> logout </button>
</center>
</body>
</head>
</html>
session_start();
include "login_session.php";
include "koneksi.php";
?>
<html>
<head>
<title> Login Berhasil </title>
<link rel="stylesheet" type="text/css" href="form.css">
<body>
<br>
<br>
<center>
Selamat Datang
<?php echo $_SESSION['username']; ?>
<br>
Terima kasih sudah login
<br>
<button onclick="location.href='login_out.php';" class="button"> logout </button>
</center>
</body>
</head>
</html>
login_session.php
<?php
if (empty($_SESSION['username']))
{
?>
<?php
echo "<meta http-equiv='refresh' content ='0; url=login.php'>";
exit;
}
?>
if (empty($_SESSION['username']))
{
?>
<?php
echo "<meta http-equiv='refresh' content ='0; url=login.php'>";
exit;
}
?>
login_out.php
<?php
session_start();
session_unset();
session_destroy();
echo "<meta http-equiv='refresh' content ='0; url=login.php'>";
exit;
?>
session_start();
session_unset();
session_destroy();
echo "<meta http-equiv='refresh' content ='0; url=login.php'>";
exit;
?>
registrasi.php
<html>
<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title> contoh registrasi </title>
<body>
<h2 class="centers"> <center> Contoh Registrasi </center> </h2>
<br>
<div class="center">
<form action="prosesregistrasi.php" method="post">
<label>Nama Lengkap </label>
<input type="text" name="nama" required="required" class="input">
<label>Username </label>
<input type="text" name="username" required="required" class="input">
<label>Password </label>
<input type="password" name="password" required="required" class="input">
<input type="submit" value="Submit" name="submit" class="button">
<input type="reset" value="reset" class="button">
</form>
</body>
</head>
</html>
<head>
<link rel="stylesheet" type="text/css" href="form.css">
<title> contoh registrasi </title>
<body>
<h2 class="centers"> <center> Contoh Registrasi </center> </h2>
<br>
<div class="center">
<form action="prosesregistrasi.php" method="post">
<label>Nama Lengkap </label>
<input type="text" name="nama" required="required" class="input">
<label>Username </label>
<input type="text" name="username" required="required" class="input">
<label>Password </label>
<input type="password" name="password" required="required" class="input">
<input type="submit" value="Submit" name="submit" class="button">
<input type="reset" value="reset" class="button">
</form>
</body>
</head>
</html>
prosesregistrasi.php
<?php
if(isset($_POST['submit']))
{
include "koneksi.php";
$nama = $_POST['nama'];
$username = $_POST['username'];
$password = $_POST['password'];
$query = "insert into user (id,nama,username,password) values ('','$nama','$username','$password')";
$result = mysql_query($query,$connection) or die("query salah : ".mysql_error());
if($result)
{
echo "registrasi berhasil";
echo "<meta http-equiv='refresh' content='1; url=login.php'>";
}
else
{
echo "registrasi gagal";
?>
<meta http-equiv='refresh' content='1; url=login.php'>;
<?php
}
}
?>
if(isset($_POST['submit']))
{
include "koneksi.php";
$nama = $_POST['nama'];
$username = $_POST['username'];
$password = $_POST['password'];
$query = "insert into user (id,nama,username,password) values ('','$nama','$username','$password')";
$result = mysql_query($query,$connection) or die("query salah : ".mysql_error());
if($result)
{
echo "registrasi berhasil";
echo "<meta http-equiv='refresh' content='1; url=login.php'>";
}
else
{
echo "registrasi gagal";
?>
<meta http-equiv='refresh' content='1; url=login.php'>;
<?php
}
}
?>