Untuk membuat login menggunakan PHP, kita memerlukan :
- tabel user
- login.php
- index.php
- logout.php
1
2
3
4
5
6
7
|
CREATE TABLE `user` (
`id_user` int(11) NOT NULL AUTO_INCREMENT,
`nama` varchar(100) DEFAULT NULL,
`username` varchar(100) DEFAULT NULL,
`password` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id_user`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
</head>
<body>
<form action="login.php" method="post">
<h2>Please sign in</h2>
<input name="username" type="text" placeholder="Username" />
<input name="password" type="password" placeholder="Password" />
<button name="submit" class="btn btn-primary" type="submit">Login</button>
<div><?php echo $error; ?></div>
</form>
</body>
</html>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
<?php
session_start();
//mengatur pesan error dengan nilai kosong
$error = "";
if(isset($_POST['submit'])){
// koneksi ke database
mysql_connect('localhost', 'usernameanda', 'passwordanda');
mysql_select_db('databaseanda');
// menangkap variable form
$username = mysql_real_escape_string(trim($_POST['username']));
$password = mysql_real_escape_string(trim($_POST['password']));
// jalankan query
$query = "SELECT * FROM user WHERE username = '$username' AND password = '$password'";
$result = mysql_query($query);
$data = mysql_fetch_array($result);
if(!$result) echo die(mysql_error ());
$jumlahbaris = mysql_num_rows($result);
// jika data sesuai
if($jumlahbaris==1){
// mendaftarkan session
$_SESSION['logged'] = "logged";
$_SESSION['id'] = $data['id_user'];
$_SESSION['nama'] = $data['nama'];
$_SESSION['username'] = $data['username'];
// redirect ke index.php
echo '<script type="text/javascript">window.location="index.php"</script>';
}else{
// jika data tidak sesuai
$error = "<div class='alert alert-error'>Username / Password Salah</div>";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Login Page</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
</head>
<body>
<form action="login.php" method="post">
<h2>Please sign in</h2>
<input name="username" type="text" placeholder="Username" />
<input name="password" type="password" placeholder="Password" />
<button name="submit" class="btn btn-primary" type="submit">Login</button>
<div><?php echo $error; ?></div>
</form>
</body>
</html>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<?php
session_start();
//jika session kosong (belum melakukan login), diredirect ke login.php
if($_SESSION['logged']<>'logged'){
// redirect ke login.php
echo '<script type="text/javascript">window.location="login.php"</script>';
}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home</title>
</head>
<body>
Selamat datang <?php echo $_SESSION['nama']; ?><br />
Ini adalah halaman home.<br />
<a href="logout.php">Logout</a>
</body>
</html>
|
1
2
3
4
5
6
7
8
9
|
<?php
session_start();
//menghapus session
session_destroy();
//redirect ke index.php
echo '<script type="text/javascript">window.location="login.php"</script>';
?>
|