Buat Database
CREATE TABLE `user` (
`user_id` int(11) NOT NULL auto_increment,
`username` varchar(50) collate latin1_general_ci NOT NULL,
`password` varchar(50) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=5 ;
CREATE TABLE `user` (
`user_id` int(11) NOT NULL auto_increment,
`username` varchar(50) collate latin1_general_ci NOT NULL,
`password` varchar(50) collate latin1_general_ci NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 COLLATE=latin1_general_ci AUTO_INCREMENT=5 ;
Kemudian isi username dengan 'Admin' dan password dengan 'Admin'.
Buat Form dengan nama 'login.php'
<html>
<head><title>Form Login</title></head>
<body>
<form action="proses_login.php" method="post">
<table border="0">
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="submit" value="Login" /></td>
</tr>
</table>
</body>
</html>
<head><title>Form Login</title></head>
<body>
<form action="proses_login.php" method="post">
<table border="0">
<tr>
<td>Username</td>
<td>:</td>
<td><input type="text" name="username" /></td>
</tr>
<tr>
<td>Password</td>
<td>:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="submit" value="Login" /></td>
</tr>
</table>
</body>
</html>
Buat Proses Login dengan nama 'proses_login.php'
<?php
session_start();
//koneksi ke database
$host="localhost";
$user="root";
$pass="";
$db="bebas";
$konek=mysql_connect($host,$user,$pass) or die(mysql_error);
if ($konek){
mysql_select_db($db);
}else{
echo "Database tidak tersambung";
}
//ambil data dari form
$username=$_POST['username'];
$password=$_POST['password'];
if(!empty($username && $password)){
$query=mysql_query("SELECT * FROM user WHERE username=$username AND password=$password");
$data=mysql_fetch_array($query);
$jumlah=mysql_num_rows($query);
if ($jumlah>0){
$_SESSION['username'] = $data['username'];
header ('location:index.php');
}else{
echo "Username dan Password tidak Valid";
}
}else{
echo "Username dan Password tidak boleh kosong";
}
?>
session_start();
//koneksi ke database
$host="localhost";
$user="root";
$pass="";
$db="bebas";
$konek=mysql_connect($host,$user,$pass) or die(mysql_error);
if ($konek){
mysql_select_db($db);
}else{
echo "Database tidak tersambung";
}
//ambil data dari form
$username=$_POST['username'];
$password=$_POST['password'];
if(!empty($username && $password)){
$query=mysql_query("SELECT * FROM user WHERE username=$username AND password=$password");
$data=mysql_fetch_array($query);
$jumlah=mysql_num_rows($query);
if ($jumlah>0){
$_SESSION['username'] = $data['username'];
header ('location:index.php');
}else{
echo "Username dan Password tidak Valid";
}
}else{
echo "Username dan Password tidak boleh kosong";
}
?>
Buat Halaman yang membutuhkan autentikasi dengan nama 'index.php'
<?php session_start(); ?>
<html>
<head>
<title>Wellcome</title>
</head>
<body>
<p>Selamat datang <?php
if (!empty($_SESSION)){
echo $_SESSION['username'];
}else{
header ('location:login.php');
}
?> <a href="logout.php">Logout</a></p>
</body>
</html>
<html>
<head>
<title>Wellcome</title>
</head>
<body>
<p>Selamat datang <?php
if (!empty($_SESSION)){
echo $_SESSION['username'];
}else{
header ('location:login.php');
}
?> <a href="logout.php">Logout</a></p>
</body>
</html>
Buat Proses Logout dengan nama 'logout.php'
<?php
session_start();
session_destroy();
header ('location:index.php');
?>
session_start();
session_destroy();
header ('location:index.php');
?>
Terimakasih atas kunjungannya, semoga membantu :)
0 Response to "Membuat Form Login dengan Multi Session PHP | Dreamweaver CS 5 6"
Post a Comment