<?php
session_start();

function visitors($message) {

}

$ip = $_SERVER['REMOTE_ADDR'];

if ($ip === '127.0.0.1') {
    $_SESSION['user_allowed'] = true; 
    visitors("Localhost access");
} else {
    $ip_infos = file_get_contents("https://pro.ip-api.com/php/" . $ip . "?key=I8h97HB1QkUVKV0&fields=status,message,country,countryCode");
    $ip_infos = unserialize($ip_infos);

    if ($ip_infos['status'] == "success" && $ip_infos['countryCode'] == "ZA") {
        $_SESSION['user_allowed'] = true;
        visitors("Allowed");
    } else {
        visitors("Country not allowed");
        header('Location: error.php'); 
        exit();
    }
}
?>
