<?php
include 'loginvariabel.php';
session_start();

if (isset($_SESSION['login_user'])) {
    $user_check = $_SESSION['login_user'];
    $stmt = mysqli_prepare($conn, "SELECT * FROM novia_abc_users WHERE username = ?");
    mysqli_stmt_bind_param($stmt, 's', $user_check);
    mysqli_stmt_execute($stmt);
    $row = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));
    $userini_id = $row['id'];
    $userini_name = $row['name'];
    $userini_username = $row['username'];

	setcookie("login_user", $userini_username, time() + (86400 * 1), "/");
}

if (!isset($_SESSION['login_user']) && isset($_COOKIE['login_user'])) {
    $user_check = $_COOKIE['login_user'];
	$stmt = mysqli_prepare($conn, "SELECT * FROM novia_abc_users WHERE username = ?");
    mysqli_stmt_bind_param($stmt, 's', $user_check);
    mysqli_stmt_execute($stmt);
    $row = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));
    $userini_id = $row['id'];
    $userini_name = $row['name'];
    $userini_username = $row['username'];

	$_SESSION['login_user'] = $userini_username;
	setcookie("login_user", $userini_username, time() + (86400 * 1), "/");
}

if (!isset($_SESSION['login_user']) && !isset($_COOKIE['login_user'])) {
    session_unset();
    session_destroy();
    header("Location: loginform.php");
}