<?php
// Set session cookie lifetime 30 hari (dalam detik)
ini_set('session.cookie_lifetime', 86400 * 30);
session_start();


// Cek apakah session user sudah ada, atau jika tidak, cek cookie 'remember_me'
if (!isset($_SESSION['user']) && isset($_COOKIE['remember_me'])) {
    // Jika session belum ada, tapi cookie remember_me ada, periksa token
    $token = $_COOKIE['remember_me'];

    // Include file koneksi ke database
    include_once "koneksi.php";

    // Cari user berdasarkan token yang ada di cookie
    $result = mysqli_query($koneksi, "SELECT * FROM user WHERE remember_token='$token'");

    // Jika ada user yang cocok, set session user
    if (mysqli_num_rows($result) > 0) {
        $_SESSION['user'] = mysqli_fetch_array($result);
    }
}

// Jika session 'user' belum ada, redirect ke halaman login
if (!isset($_SESSION['user'])) {
    header('Location: login.php');
    exit();
}
?>