88 lines
2.7 KiB
PHP
Executable File
88 lines
2.7 KiB
PHP
Executable File
<?php
|
|
// Function to get visitor's IP address
|
|
function getVisitorIP() {
|
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
|
// Check for shared IP
|
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
// Check for proxy IP (use the first IP in the list)
|
|
$ipList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
|
$ip = trim($ipList[0]); // Use the first IP address
|
|
} elseif (!empty($_SERVER['HTTP_X_REAL_IP'])) {
|
|
// Check for real IP (e.g., from CDN)
|
|
$ip = $_SERVER['HTTP_X_REAL_IP'];
|
|
} else {
|
|
// Default IP
|
|
$ip = $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
return $ip;
|
|
}
|
|
|
|
// Function to get location from IP address using ip-api
|
|
function getLocationFromIP($ip) {
|
|
$url = "http://ip-api.com/json/{$ip}";
|
|
|
|
$response = @file_get_contents($url);
|
|
$data = json_decode($response, true);
|
|
|
|
if (isset($data['country'])) {
|
|
return $data['country'];
|
|
} else {
|
|
return 'Unknown';
|
|
}
|
|
}
|
|
|
|
// Function to write logs to file in Chinese
|
|
function writeLog($logMessage) {
|
|
$logFile = __DIR__ . '/request_logs.txt'; // Log file path
|
|
|
|
// Prepare the log message with timestamp
|
|
$logMessage = "[" . date('Y-m-d H:i:s') . "] " . $logMessage . "\n";
|
|
|
|
// Write the log message to the file
|
|
file_put_contents($logFile, $logMessage, FILE_APPEND);
|
|
}
|
|
|
|
// Get visitor's IP address
|
|
$ip = getVisitorIP();
|
|
|
|
// Get location country
|
|
$country = getLocationFromIP($ip);
|
|
|
|
// Log request headers and IP address
|
|
$headers = apache_request_headers(); // Get all request headers
|
|
|
|
// Format headers for logging in Chinese
|
|
$headersLog = "访问者IP地址: " . $ip . "\n";
|
|
$headersLog .= "访问者国家: " . $country . "\n";
|
|
$headersLog .= "请求头信息:\n";
|
|
|
|
foreach ($headers as $header => $value) {
|
|
$headersLog .= "$header: $value\n";
|
|
}
|
|
|
|
// API 返回日志信息
|
|
$apiResponseLog = "调用接口结果: " . ($country == 'China' ? "中国访问" : ($country == 'United States' ? "美国访问" : "其他国家访问")) . "\n";
|
|
|
|
// Write headers, country info, and API result to the log file
|
|
writeLog($headersLog);
|
|
writeLog($apiResponseLog);
|
|
|
|
// 301 重定向到不同域名
|
|
if ($country == "China") {
|
|
writeLog("重定向到: /cn/products\n");
|
|
header('HTTP/1.1 301 Moved Permanently');
|
|
header('Location: /cn/products');
|
|
} elseif ($country == "United States") {
|
|
writeLog("重定向到: /en/products\n");
|
|
header('HTTP/1.1 301 Moved Permanently');
|
|
header('Location: /en/products');
|
|
} else {
|
|
writeLog("重定向到: /en/products (默认英文)\n");
|
|
header('HTTP/1.1 301 Moved Permanently');
|
|
header('Location: /en/products'); // Default to English
|
|
}
|
|
|
|
exit(); // Stop the script after redirecting
|
|
?>
|