I have this method that has login logic, but when I tried to set cookies to something it store, But the issue is on redirection when I close the browser it redirect me back to login form even if I checked the remember checkbox input, Here is my code
<?php
public function login()
{
$stmt = $this->db->prepare("SELECT userid FROM users WHERE username = :username AND password = :password");
$stmt->execute(array(
':username' => $_POST['username'],
':password' => md5($_POST['password'])
));
$data = $stmt->fetch(PDO::FETCH_ASSOC);
$count = $stmt->rowCount();
if($count > 0){
Session::init();
Session::set('loggedIn', true);
Session::set('userId', $data['userId']);
$duration = time() + 3600 * 24 * 30;
if ( isset($_POST['rememberme']) ) {
setcookie('loggedIn', Session::get('loggedIn'), $duration );
setcookie('userId', Session::get('userId'), $hour );
header('location: '. URL .'/homedashboard');
// echo $_COOKIE['userId'];
} else {
setcookie('loggedIn', "" );
setcookie('userId', "" );
header('location: '. URL .'/homedashboard');
}
} else {
header('location: '. URL .'/Login');
}
}