Berikut peningkatan terakhir: jika terdeteksi bot, maka langsung diarahkan ke https://www.youtube.com. Kita akan gunakan 3 file terpisah agar lebih terstruktur:
config.php
functions.php
index.php
1. config.php
php
<?php
return [
// Daftar bot berdasarkan User-Agent
'bot_list' => [
'Googlebot', 'Bingbot', 'Slurp', 'DuckDuckBot', 'Baiduspider',
'YandexBot', 'Sogou', 'Exabot', 'facebot', 'ia_archiver',
'SemrushBot', 'AhrefsBot', 'PetalBot', 'Applebot', 'LinkedInBot'
],
// URL pengalihan untuk pengguna manusia
'redirect_url' => 'https://example.com/affiliate-link',
// URL pengalihan untuk bot
'bot_redirect_url'=> 'https://www.youtube.com',
// Pesan fallback (jika ingin digunakan)
'block_message' => 'Maaf, konten ini hanya untuk pengguna manusia.',
// Whitelist IP atau User-Agent
'whitelist' => ['123.45.67.89', 'MyCustomUserAgent'],
// Rentang IP resmi Googlebot (digunakan untuk verifikasi IP)
'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'
]
];
2. functions.php
php
<?php
/**
* Cek apakah visitor adalah bot
*/
function isBot() {
$config = include __DIR__ . '/config.php';
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
$remoteIp = $_SERVER['REMOTE_ADDR'] ?? '';
// 1) Whitelist IP/User-Agent
if (in_array($remoteIp, $config['whitelist'], true) ||
in_array($userAgent, $config['whitelist'], true)) {
return false;
}
// 2) Cek daftar User-Agent bot
foreach ($config['bot_list'] as $bot) {
if (stripos($userAgent, $bot) !== false) {
return true;
}
}
// 3) Verifikasi IP Googlebot
if (isGoogleBotIP($remoteIp, $config['googlebot_ip_ranges'])) {
return true;
}
return false;
}
/**
* Cek apakah IP termasuk ke dalam suatu subnet (CIDR)
*/
function ip_in_range(string $ip, string $range): bool {
list($subnet, $mask) = explode('/', $range);
$ip = ip2long($ip);
$sub = ip2long($subnet);
$mask = ~((1 << (32 - (int)$mask)) - 1);
return ($ip & $mask) === ($sub & $mask);
}
/**
* Loop untuk verifikasi Googlebot IP
*/
function isGoogleBotIP(string $ip, array $ranges): bool {
foreach ($ranges as $range) {
if (ip_in_range($ip, $range)) {
return true;
}
}
return false;
}
/**
* Log aktivitas deteksi atau redirect
*/
function logActivity(bool $isBot) {
$type = $isBot ? 'BOT detected & redirected' : 'User redirected';
$msg = date('Y-m-d H:i:s') . " - {$type} - IP: {$_SERVER['REMOTE_ADDR']}" . PHP_EOL;
file_put_contents(__DIR__ . '/bot_detection.log', $msg, FILE_APPEND);
}
3. index.php
php
<?php
require __DIR__ . '/functions.php';
$config = include __DIR__ . '/config.php';
// Cek bot
if (isBot()) {
logActivity(true);
// Redirect bot ke YouTube
header("Location: " . $config['bot_redirect_url'], true, 302);
exit;
}
// Bukan bot, redirect ke affiliate link
logActivity(false);
// Hindari caching
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
header("Location: " . $config['redirect_url'], true, 302);
exit;
Dengan struktur ini:
Bot terdeteksi akan langsung diarahkan ke www.youtube.com.
Pengguna manusia tetap diarahkan ke link afiliasi.