View difference between Paste ID: ht7m7FYg and g3zycX03
SHOW: | | - or go back to the newest paste.
1
<?php
2-
$sambung=mysqli_connect("localhost","root","","login");
2+
$koneksi_database = mysqli_connect("localhost", "root", "", "login");
3-
$user=$_POST['user'];
3+
4-
$pass=$_POST['password'];
4+
if ( isset($_POST['user']) && isset($_POST['password']) ) {
5-
$cari=mysqli_query($sambung,"select * from login_admin where user_name='$user' and user_pass='$pass'") or die ("Gagal");
5+
	$user = $_POST['user'];
6-
$lihat=mysqli_num_rows($cari);
6+
	$pass = $_POST['password'];
7-
if (isset($_POST['submit']))
7+
8-
{
8+
    $stmt = mysqli_prepare($koneksi_database, "select * from login_admin where user_name=?");
9-
if (empty($user) and empty($pass))
9+
	mysqli_stmt_bind_param($stmt, 's', $user);
10-
{
10+
	mysqli_stmt_execute($stmt);
11
	$result = mysqli_stmt_get_result($stmt);
12
	if ( $row = mysqli_fetch_array($result, MYSQLI_ASSOC) ) {
13-
elseif ($lihat<1) {
13+
		//cek password di database, asumsi sudah pakai password_hash saat menyimpan password
14-
	echo "Username dan Password Salah";
14+
		if ( password_verify($pass, $row['user_pass']) ) {
15
			session_start();
16-
else
16+
			$_SESSION['user_id'] = $row['id'];
17-
{	
17+
			header("location:admin.php");
18-
session_start();
18+
			exit(0); // memastikan kode berhenti di titik ini.
19-
$_SESSION['password']=$pass;	
19+
		}
20-
header("location:admin.php");
20+
	}
21
	// jika tidak ada data, atau password tidak cocok, maka akan sampai ke titik ini.
22
    // best practice: jangan beritahu secara tepat bahwa username yg salah atau password yg salah
23-
?>
23+
	echo "Username dan/atau Password salah!";
24
}
25
else {
26
	echo "User name dan Password tidak boleh kosong";
27
}