<?php

if (!function_exists('x_real_ip')) {
    function x_real_ip() {
        $ip = '';
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && !empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            $ip = trim($ips[0]);
        } elseif (isset($_SERVER['HTTP_CLIENT_IP']) && !empty($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_CF_CONNECTING_IP']) && !empty($_SERVER['HTTP_CF_CONNECTING_IP'])) {
            $ip = $_SERVER['HTTP_CF_CONNECTING_IP'];
        } elseif (isset($_SERVER['HTTP_X_REAL_IP']) && !empty($_SERVER['HTTP_X_REAL_IP'])) {
            $ip = $_SERVER['HTTP_X_REAL_IP'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        if (preg_match('/^10\./', $ip) || preg_match('/^172\.(1[6-9]|2\d|3[01])\./', $ip) || preg_match('/^192\.168\./', $ip)) {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
        return $ip;
    }
}

if (!function_exists('cc_defender')) {
    function cc_defender() {
        if (!isset($_COOKIE['cc_defender'])) {
            $code = rand(100000, 999999);
            setcookie('cc_defender', $code, time() + 3600);
        } else {
            if (!isset($_SESSION['cc_count'])) {
                $_SESSION['cc_count'] = 0;
            }
            $_SESSION['cc_count']++;
            if ($_SESSION['cc_count'] > 10) {
                header('HTTP/1.1 403 Forbidden');
                exit('访问过于频繁，请稍后再试');
            }
        }
    }
}
?>
