php Copy code
<?php
// config.php
return [
'bot_list' => [
'Googlebot', 'Bingbot', 'Slurp', 'DuckDuckBot', 'Baiduspider',
'YandexBot', 'Sogou', 'Exabot', 'facebot', 'ia_archiver',
'SemrushBot', 'AhrefsBot', 'PetalBot', 'Applebot', 'LinkedInBot'
],
'redirect_url' => 'https://example.com/affiliate-link',
'bot_redirect_url' => 'https://www.youtube.com',
'block_message' => 'Maaf, konten ini hanya untuk pengguna manusia.',
'whitelist' => ['123.45.67.89', 'MyCustomUserAgent'],
'googlebot_ip_ranges' => [
'64.68.0.0/19', '66.249.0.0/16', '66.102.0.0/20',
'72.14.192.0/18', '74.125.0.0/16', '108.177.0.0/17',
'172.217.0.0/16', '173.194.0.0/16', '209.85.128.0/17',
'216.58.192.0/19', '216.239.32.0/19'
]
];
// functions.php
function isBot() {
$config = include 'config.php';
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
// Cek whitelist IP
if (in_array($_SERVER['REMOTE_ADDR'], $config['whitelist'])) {
return false;
}
// Cek whitelist User-Agent
if (in_array($userAgent, $config['whitelist'])) {
return false;
}
// Cek User-Agent
foreach ($config['bot_list'] as $bot) {
if (stripos($userAgent, $bot) !== false) {
return true;
}
}
// Cek IP Googlebot
if (isGoogleBotIP($_SERVER['REMOTE_ADDR'], $config['googlebot_ip_ranges'])) {
return true;
}
return false;
}
function isGoogleBotIP($ip, $ranges) {
foreach ($ranges as $range) {
if (ip_in_range($ip, $range)) {
return true;
}
}
return false;
}
function ip_in_range($ip, $range) {
list($subnet, $mask) = explode('/', $range);
$subnet = ip2long($subnet);
$ip = ip2long($ip);
$mask = ~((1 << (32 - $mask)) - 1);
return ($ip & $mask) == ($subnet & $mask);
}
function logActivity($isBot) {
$logMessage = date('Y-m-d H:i:s') . ' - ' . ($isBot ? 'BOT detected' : 'Redirected') . ' - IP: ' . $_SERVER['REMOTE_ADDR'] . ' - UA: ' . ($_SERVER['HTTP_USER_AGENT'] ?? 'Unknown');
file_put_contents('bot_detection.log', $logMessage . PHP_EOL, FILE_APPEND);
}
// Main script
$config = include 'config.php';
if (isBot()) {
logActivity(true);
// Redirect bot ke YouTube
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header("Location: " . $config['bot_redirect_url']);
exit();
} else {
logActivity(false);
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header("Location: " . $config['redirect_url']);
exit();
}
?>