<?php
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

require_once "config.php";

$session_id = session_id();
$ip = $_SERVER['REMOTE_ADDR'] ?? '';
$user_agent = $_SERVER['HTTP_USER_AGENT'] ?? '';

$cek = $conn->query("
SELECT id 
FROM visitors
WHERE session_id='$session_id'
");

if($cek->num_rows == 0){

    $conn->query("
    INSERT INTO visitors
    (session_id, ip_address, user_agent, created_at)
    VALUES
    ('$session_id','$ip','$user_agent',NOW())
    ");

}else{

    $conn->query("
    UPDATE visitors
    SET created_at = NOW()
    WHERE session_id='$session_id'
    ");
}
?>