langsung aja,pertama kita harus membuat Databesnya lihat gambar dibawah ini :
Kalau Sobat Gak mau ribet saya sudah sediakan databasenya disini
Nah, sekarang kita masuk ke scriptnya, kita membuat beberapa file seperti :
index.php
connect.php
ceklogin.php
user.php
administrator.php
logout.php
Script Index.php, ini sebagai form Login dihalaman
<html>
<head>
<title>.: Admin Room Toko Buku Bekas :.</title>
<link href="css/signInStyle.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div id="note">
<center>
Welcome To Admin
</center>
</div>
<div id="header">
<img src="images/lock.png" border="0" class="image">
<div class="title">Sign In Please</div><!-- title //-->
</div><!-- header //-->
<div id="formTable">
<div class="menuLabel">
<form method="post" action="ceklogin.php?op=in" onSubmit="return validasiForm(this)">
Username :<br>
<input type="text" class="inputText" size="34" name="userid"><br>
Password :<br>
<input type="password" class="inputText" size="34" name="psw"><br>
<input type="submit" class="button" value="Sign In"><input type="reset" class="button" value="Cancel" onClick="window.location = '../'">
</form>
</div>
</div><!-- formTable //-->
</div><!-- wrapper //-->
</body>
</html>
<head>
<title>.: Admin Room Toko Buku Bekas :.</title>
<link href="css/signInStyle.css" type="text/css" rel="stylesheet">
</head>
<body>
<div id="wrapper">
<div id="note">
<center>
Welcome To Admin
</center>
</div>
<div id="header">
<img src="images/lock.png" border="0" class="image">
<div class="title">Sign In Please</div><!-- title //-->
</div><!-- header //-->
<div id="formTable">
<div class="menuLabel">
<form method="post" action="ceklogin.php?op=in" onSubmit="return validasiForm(this)">
Username :<br>
<input type="text" class="inputText" size="34" name="userid"><br>
Password :<br>
<input type="password" class="inputText" size="34" name="psw"><br>
<input type="submit" class="button" value="Sign In"><input type="reset" class="button" value="Cancel" onClick="window.location = '../'">
</form>
</div>
</div><!-- formTable //-->
</div><!-- wrapper //-->
</body>
</html>
Setelah itu kita buat untuk file script koneksi ke databasenaya sbb :
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'demo_multilogin';
mysql_select_db($dbname);
?>
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
$dbname = 'demo_multilogin';
mysql_select_db($dbname);
?>
Simpan dengan nama connect.php didalam sub folder dengan nama folder config
Berikutnya adalah Script untuk proses mengcek ke tabel Login sesua dengan levelnya masing2 scriptnya sbb:
<?php
include("config/connect.php");
session_start();
$userid = $_POST['userid'];
$psw = $_POST['psw'];
$op = $_GET['op'];
if ($userid==""){
echo "<script>alert('Silakan masukkan username Anda.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
}
elseif ($psw=="") {
echo "<script>alert('Silakan masukkan password Anda.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
}
$cekuser = "SELECT COUNT(*) AS `ctr` FROM `login` WHERE `userid`='$userid'";
$datacekuser = mysql_query($cekuser);
while ($rscekuser = mysql_fetch_array($datacekuser)) {
$ctr = $rscekuser['ctr'];
}
if ($ctr==0) {
echo "<script>alert('Username yang Anda masukkan salah.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
}
if($op=="in"){
$cek = mysql_query("SELECT * FROM login WHERE userid='$userid' AND password='$psw'");
if(mysql_num_rows($cek)==1){//jika berhasil akan bernilai 1
$c = mysql_fetch_array($cek);
$_SESSION['userid'] = $c['userid'];
$_SESSION['level'] = $c['level'];
if($c['level']=="1"){
echo "<script>alert('Welcome to Administrator')</script>";
echo "<script>window.location = 'administrator.php'</script>";
}else if($c['level']=="2"){
echo "<script>alert('Welcome to Page User')</script>";
echo "<script>window.location = 'user.php'</script>";
}
}else{
echo "<script>alert('Password yang Anda masukkan salah.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
}
}else if($op=="out"){
unset($_SESSION['userid']);
unset($_SESSION['level']);
header("location:index.php");
}
elseif ($userid==$rs['userid'] && $psw!=$rs['psw']) {
echo "<script>alert('Password yang Anda masukkan salah.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
//header("location:admin.php?erradmin=Password yang Anda masukkan salah.");
}
?>
include("config/connect.php");
session_start();
$userid = $_POST['userid'];
$psw = $_POST['psw'];
$op = $_GET['op'];
if ($userid==""){
echo "<script>alert('Silakan masukkan username Anda.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
}
elseif ($psw=="") {
echo "<script>alert('Silakan masukkan password Anda.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
}
$cekuser = "SELECT COUNT(*) AS `ctr` FROM `login` WHERE `userid`='$userid'";
$datacekuser = mysql_query($cekuser);
while ($rscekuser = mysql_fetch_array($datacekuser)) {
$ctr = $rscekuser['ctr'];
}
if ($ctr==0) {
echo "<script>alert('Username yang Anda masukkan salah.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
}
if($op=="in"){
$cek = mysql_query("SELECT * FROM login WHERE userid='$userid' AND password='$psw'");
if(mysql_num_rows($cek)==1){//jika berhasil akan bernilai 1
$c = mysql_fetch_array($cek);
$_SESSION['userid'] = $c['userid'];
$_SESSION['level'] = $c['level'];
if($c['level']=="1"){
echo "<script>alert('Welcome to Administrator')</script>";
echo "<script>window.location = 'administrator.php'</script>";
}else if($c['level']=="2"){
echo "<script>alert('Welcome to Page User')</script>";
echo "<script>window.location = 'user.php'</script>";
}
}else{
echo "<script>alert('Password yang Anda masukkan salah.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
}
}else if($op=="out"){
unset($_SESSION['userid']);
unset($_SESSION['level']);
header("location:index.php");
}
elseif ($userid==$rs['userid'] && $psw!=$rs['psw']) {
echo "<script>alert('Password yang Anda masukkan salah.')</script>";
echo "<script>window.location = 'index.php'</script>";
exit();
//header("location:admin.php?erradmin=Password yang Anda masukkan salah.");
}
?>
Keterangan : Disini saya menggunakan 2 level yaitu admin 1(level 1) dan user(level 2)
jika sobat ingin menambahkan admin 2, sobat copas yang saya garis bawahi letakkan persis dibawahnya, sesuaikan levelnya dengan level yg ada didatabase misalnya ada level 3 maka sobat tinggal ganti ($c['level']=="3")
window.location adalah untuk page yang akan dituju level tersebut.
selanjutnya untuk script administrator.php
<?php
require_once('config/connect.php');
session_start();
//cek apakah user sudah login
if(!isset($_SESSION['userid'])){
?><script language="javascript">
alert("Maaf, Anda belum login!!");
document.location="login.php";
</script>
<?
}
//cek level user
if($_SESSION['level']!="1"){
?><script language="javascript">
alert("Anda tidak bisa mengakses page ini!!");
document.location="javascript:history.back(1)";
</script>
<?
}
?>
<html>
<head>
<title>Administrator</title>
</head>
<body>
<div align="center"><a href="user.php">User</a> | <a href="logout.php">Logout</a></div>
<div align="center" style="margin-top:50px">Ini Halaman Administrator</div>
</body>
</html>
require_once('config/connect.php');
session_start();
//cek apakah user sudah login
if(!isset($_SESSION['userid'])){
?><script language="javascript">
alert("Maaf, Anda belum login!!");
document.location="login.php";
</script>
<?
}
//cek level user
if($_SESSION['level']!="1"){
?><script language="javascript">
alert("Anda tidak bisa mengakses page ini!!");
document.location="javascript:history.back(1)";
</script>
<?
}
?>
<html>
<head>
<title>Administrator</title>
</head>
<body>
<div align="center"><a href="user.php">User</a> | <a href="logout.php">Logout</a></div>
<div align="center" style="margin-top:50px">Ini Halaman Administrator</div>
</body>
</html>
script user.php
<?php
require_once('config/connect.php');
session_start();
//cek apakah user sudah login
if(!isset($_SESSION['userid'])){
?><script language="javascript">
alert("Maaf, Anda belum login!!");
document.location="login.php";
</script>
<?
}
//cek level user
if($_SESSION['level']!="2"){
?><script language="javascript">
alert("Anda tidak bisa mengakses page ini!!");
document.location="javascript:history.back(1)";
</script>
<?
}
?>
<html>
<head>
<title>Administrator</title>
</head>
<body>
<div align="center"><a href="administrator.php">Admin</a> | <a href="logout.php">Logout</a></div>
<div align="center" style="margin-top:50px">Ini Halaman User</div>
</body>
</html>
require_once('config/connect.php');
session_start();
//cek apakah user sudah login
if(!isset($_SESSION['userid'])){
?><script language="javascript">
alert("Maaf, Anda belum login!!");
document.location="login.php";
</script>
<?
}
//cek level user
if($_SESSION['level']!="2"){
?><script language="javascript">
alert("Anda tidak bisa mengakses page ini!!");
document.location="javascript:history.back(1)";
</script>
<?
}
?>
<html>
<head>
<title>Administrator</title>
</head>
<body>
<div align="center"><a href="administrator.php">Admin</a> | <a href="logout.php">Logout</a></div>
<div align="center" style="margin-top:50px">Ini Halaman User</div>
</body>
</html>
terakhir adalah script logout.php
<? session_start();
if (session_is_registered('level'))
{
session_unregister("userid");
session_unregister("level");
//session_destroy();
?><script language="javascript">
alert("Anda Berhasil logout.");
document.location="./";
</script><?
}else{
?><script language="javascript">
alert("Anda Berhasil logout.");
document.location="./";
</script>
<?
}
?>
if (session_is_registered('level'))
{
session_unregister("userid");
session_unregister("level");
//session_destroy();
?><script language="javascript">
alert("Anda Berhasil logout.");
document.location="./";
</script><?
}else{
?><script language="javascript">
alert("Anda Berhasil logout.");
document.location="./";
</script>
<?
}
?>
Simpan dengan nama logout.php
Oiyaa, untuk tataletak didalam folder htdocs silahkan lihat dibawah ini
ok, selamat mencobat sobat......
sperti biasa saya sudah siapkan Source lengkapnya disini Download
Tidak ada komentar:
Posting Komentar