Cookie tidak jalan

saya punya website forum dan tamu bisa melihat poastingan tanpa harus login. dan saya mengekapi fitur login dengan cookie. setelah user masuk dan ceklis cookie maka di halaman home akan muncul nama user. setelah user close browser dan masuk lagi kok nama usernya hilang dan berubah jadi tamu ? tapi cookienya masih ada. ini kodenya

<?php
session_start();
require 'php/koneksi.php';
//cek cookie
if(isset($_COOKIE['id']) && isset($_COOKIE['makan'])){
	$id = $_COOKIE['id'];
	$makan = $_COOKIE['makan'];

	//ambil username berdasarkan id
	$result = mysqli_query($conn, "SELECT username FROM users WHERE id_user = $id");
	$row = mysqli_fetch_assoc($result);

	//cek cookie dan username
	if($makan === hash('sha256', $row['username'])){
		$_SESSION['login'] = true;
	}
}

if(isset($_SESSION['login'])){
	header("Location: index.php");
	exit;
}

if(isset($_POST['login'])){
	$username = $_POST['username'];
	$password = $_POST['password'];

	 $query =  mysqli_query($conn, "SELECT * FROM users WHERE username = '$username'");

	 if(mysqli_num_rows($query) === 1){
		 $row = mysqli_fetch_assoc($query);
	 	if(password_verify($password, $row["password"])){
	 		if($row['aktif'] === 'Y'){
	 			//set session
		 		$_SESSION['login'] = true;
		 		$_SESSION['id_user'] = $row['id_user'];
				$_SESSION['nama'] = $row['nama'];
				$_SESSION['username'] = $row['username'];
				$_SESSION['avatar'] = $row['avatar'];
				$_SESSION['deskripsi'] = $row['deskripsi'];

				//cek remember me
				if(isset($_POST['ingat'])){
					//set cookie
					setcookie('id', $row['id_user'], time()+60);
					setcookie('makan', hash('sha256', $row['username']), time()+60);
				}

			 	header("Location: index.php");
			 	exit;
			 	}else{
			 		$aktiferror = true;
			 	}


	 		}else{
	 			$passerror = true;
	 		}

	 }else{
	 	$loginerror = true;
	 }


}

 ?>

avatar tonisuwendi
@tonisuwendi

169 Kontribusi 36 Poin

Diperbarui 5 tahun yang lalu

Belum ada Jawaban. Jadi yang pertama Jawaban

Login untuk ikut Jawaban