Login Multi User PHP Mysqli Dengan Dropdown, ada salah satu temen ane nanyain ke ane.. dan ane cari-cari susah banget, berhubung ane punya kemampuan sedikit tentang pemrograman PHP maka coba ane cari dan ane edit2 lagi.. dan ternyata berhasil. inget y ini cuman login. gak ada CRUDnya.
jadi gini logikanya.. kalau ada seseorang login dan dia ingin ke admin maka isi form dan pilih admin, jika berhasil maka akan masuk ke halaman admin. begitu juga dengan user. namun jika salah maka akan menyuruh mengulang lagi..
oh y, ada tambahan botstrap juga.
isi file terdiri dari index.php untuk user, login.php untuk login, admin.php untuk admin, logout.php untuk keluar dan terakhir multiloginuser.sql untuk database dan tabelnya.
Login.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Session</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</head>
<body>
<p>
</p>
<div class="container">
<?php
$username=$_POST['username'];
$password=md5($_POST['password']);
$login=$_POST['login'];
if(isset($login)){
$mysqli = mysqli_connect("localhost","root","","multiloginuser");
if (mysqli_connect_errno()){
echo "Koneksi database gagal : " . mysqli_connect_error();
}
$res = $mysqli->query("SELECT * FROM login where username='$username' and password='$password'");
$row = $res->fetch_assoc();
$name = $row['name_login'];
$user = $row['username'];
$pass = $row['password'];
$type = $row['type_login'];
if($user==$username && $pass=$password){
session_start();
if($type=="admin"){
$_SESSION['mysesi']=$name;
$_SESSION['mytype']=$type;
echo "<script>window.location.assign('admin.php')</script>";
} else if($type=="user"){
$_SESSION['mysesi']=$name;
$_SESSION['mytype']=$type;
echo "<script>window.location.assign('index.php')</script>";
} else{
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<strong>Maaf!</strong> Tidak sesuai dengan type user.
</div>
<?php
}
} else{
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<strong>Warning!</strong> This username or password not same with database.
</div>
<?php
}
}
?>
<div class="panel panel-default">
<div class="panel-body">
<h2>Login Session</h2>
<form role="form" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<div class="form-group">
<select name="type_login">
<option value="admin">admin</option>
<option value="user">user</option>
</select>
</div>
<button type="submit" name="login" class="btn btn-primary">Login</button>
</form>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
</body>
</html>
index.php
<?php
session_start();
if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='user')
{
echo "<script>window.location.assign('login.php')</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Session</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</head>
<body>
<p>
</p>
<div class="container">
<div class="jumbotron">
<h1>Welcome, hi <?php echo $_SESSION['mysesi'] ?></h1>
<p>This is application login session</p>
<p><a class="btn btn-primary btn-lg" href="logout.php" role="button">Logout</a></p>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
</body>
</html>
admin.php
<?php
session_start();
if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='admin')
{
echo "<script>window.location.assign('login.php')</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Session</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</head>
<body>
<p>
</p>
<div class="container">
<div class="jumbotron">
<h1>Welcome, hi <?php echo $_SESSION['mysesi'] ?></h1>
<p>This is application login session</p>
<p><a class="btn btn-primary btn-lg" href="logout.php" role="button">Logout</a></p>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
</body>
</html>
logout.php
<?php
session_start();
echo "<script>window.location.assign('login.php')</script>";
session_destroy();
?>
multiloginuser.sql
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE `login` (
`id_login` int(11) NOT NULL,
`name_login` varchar(45) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`type_login` enum('admin','user') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `login` (`id_login`, `name_login`, `username`, `password`, `type_login`) VALUES
(1, 'admin', 'admin', '202cb962ac59075b964b07152d234b70', 'admin'),
(2, 'user', 'user', '250cf8b51c773f3f8dc8b4be867a9a02', 'user');
jadi gini logikanya.. kalau ada seseorang login dan dia ingin ke admin maka isi form dan pilih admin, jika berhasil maka akan masuk ke halaman admin. begitu juga dengan user. namun jika salah maka akan menyuruh mengulang lagi..
oh y, ada tambahan botstrap juga.
isi file terdiri dari index.php untuk user, login.php untuk login, admin.php untuk admin, logout.php untuk keluar dan terakhir multiloginuser.sql untuk database dan tabelnya.
Login.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Session</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</head>
<body>
<p>
</p>
<div class="container">
<?php
$username=$_POST['username'];
$password=md5($_POST['password']);
$login=$_POST['login'];
if(isset($login)){
$mysqli = mysqli_connect("localhost","root","","multiloginuser");
if (mysqli_connect_errno()){
echo "Koneksi database gagal : " . mysqli_connect_error();
}
$res = $mysqli->query("SELECT * FROM login where username='$username' and password='$password'");
$row = $res->fetch_assoc();
$name = $row['name_login'];
$user = $row['username'];
$pass = $row['password'];
$type = $row['type_login'];
if($user==$username && $pass=$password){
session_start();
if($type=="admin"){
$_SESSION['mysesi']=$name;
$_SESSION['mytype']=$type;
echo "<script>window.location.assign('admin.php')</script>";
} else if($type=="user"){
$_SESSION['mysesi']=$name;
$_SESSION['mytype']=$type;
echo "<script>window.location.assign('index.php')</script>";
} else{
?>
<div class="alert alert-warning alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<strong>Maaf!</strong> Tidak sesuai dengan type user.
</div>
<?php
}
} else{
?>
<div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<strong>Warning!</strong> This username or password not same with database.
</div>
<?php
}
}
?>
<div class="panel panel-default">
<div class="panel-body">
<h2>Login Session</h2>
<form role="form" method="post">
<div class="form-group">
<label for="username">Username</label>
<input type="text" class="form-control" id="username" name="username">
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" class="form-control" id="password" name="password">
</div>
<div class="form-group">
<select name="type_login">
<option value="admin">admin</option>
<option value="user">user</option>
</select>
</div>
<button type="submit" name="login" class="btn btn-primary">Login</button>
</form>
</div>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
</body>
</html>
index.php
<?php
session_start();
if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='user')
{
echo "<script>window.location.assign('login.php')</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Session</title>
<!-- Bootstrap -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</head>
<body>
<p>
</p>
<div class="container">
<div class="jumbotron">
<h1>Welcome, hi <?php echo $_SESSION['mysesi'] ?></h1>
<p>This is application login session</p>
<p><a class="btn btn-primary btn-lg" href="logout.php" role="button">Logout</a></p>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
</body>
</html>
admin.php
<?php
session_start();
if (!isset($_SESSION['mysesi']) && !isset($_SESSION['mytype'])=='admin')
{
echo "<script>window.location.assign('login.php')</script>";
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Login Session</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/css/bootstrap.min.css" integrity="sha384-MCw98/SFnGE8fJT3GXwEOngsV7Zt27NXFoaoApmYm81iuXoPkFOJwJ8ERdknLPMO" crossorigin="anonymous">
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.3/js/bootstrap.min.js" integrity="sha384-ChfqqxuZUCnJSK3+MXmPNIyE6ZbWh2IMqE241rYiqJxyMiZ6OW/JmZQ5stwEULTy" crossorigin="anonymous"></script>
</head>
<body>
<p>
</p>
<div class="container">
<div class="jumbotron">
<h1>Welcome, hi <?php echo $_SESSION['mysesi'] ?></h1>
<p>This is application login session</p>
<p><a class="btn btn-primary btn-lg" href="logout.php" role="button">Logout</a></p>
</div>
</div>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.3/umd/popper.min.js" integrity="sha384-ZMP7rVo3mIykV+2+9J3UJ46jBk0WLaUAdn689aCwoqbBJiSnjAK/l8WvCWPIPm49" crossorigin="anonymous"></script>
</body>
</html>
logout.php
<?php
session_start();
echo "<script>window.location.assign('login.php')</script>";
session_destroy();
?>
multiloginuser.sql
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";
CREATE TABLE `login` (
`id_login` int(11) NOT NULL,
`name_login` varchar(45) NOT NULL,
`username` varchar(100) NOT NULL,
`password` varchar(100) NOT NULL,
`type_login` enum('admin','user') NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `login` (`id_login`, `name_login`, `username`, `password`, `type_login`) VALUES
(1, 'admin', 'admin', '202cb962ac59075b964b07152d234b70', 'admin'),
(2, 'user', 'user', '250cf8b51c773f3f8dc8b4be867a9a02', 'user');