Khối không mong muốn Robot/Nhện khách qua PHP
Hướng dẫn:
Đặt PHP Code sau vào đầu file index.php của bạn.
Ý tưởng ở đây là đặt mã vào trang chủ PHP của trang web chính, điểm vào chính của trang web.
Nếu bạn có các tệp PHP khác được truy cập trực tiếp qua URL (không bao gồm PHP bao gồm hoặc yêu cầu tệp loại hỗ trợ), sau đó đặt mã ở đầu các tệp đó. Đối với hầu hết các trang web PHP và các trang web PHP CMS, tệp index.php của root là tệp là điểm vào chính của trang web.
Hãy nhớ rằng số liệu thống kê trang web của bạn, tức là AWStats, sẽ vẫn ghi lại lần truy cập trong rô bốt không xác định (được xác định bởi 'bot' theo sau là dấu cách hoặc một trong các ký tự sau _ +:,.;/-) các bot này sẽ bị chặn truy cập vào nội dung trang web của bạn.
<?php
// ---------------------------------------------------------------------------------------------------------------
// Banned IP Addresses and Bots - Redirects banned visitors who make it past the .htaccess and or robots.txt files to an URL.
// The $banned_ip_addresses array can contain both full and partial IP addresses, i.e. Full = 123.456.789.101, Partial = 123.456.789. or 123.456. or 123.
// Use partial IP addresses to include all IP addresses that begin with a partial IP addresses. The partial IP addresses must end with a period.
// The $banned_bots, $banned_unknown_bots, and $good_bots arrays should contain keyword strings found within the User Agent string.
// The $banned_unknown_bots array is used to identify unknown robots (identified by 'bot' followed by a space or one of the following characters _+:,.;/\-).
// The $good_bots array contains keyword strings used as exemptions when checking for $banned_unknown_bots. If you do not want to utilize the $good_bots array such as
// $good_bots = array(), then you must remove the the keywords strings 'bot.','bot/','bot-' from the $banned_unknown_bots array or else the good bots will also be banned.
$banned_ip_addresses = array('41.','64.79.100.23','5.254.97.75','148.251.236.167','88.180.102.124','62.210.172.77','45.','195.206.253.146');
$banned_bots = array('.ru','AhrefsBot','crawl','crawler','DotBot','linkdex','majestic','meanpath','PageAnalyzer','robot','rogerbot','semalt','SeznamBot','spider');
$banned_unknown_bots = array('bot ','bot_','bot+','bot:','bot,','bot;','bot\\','bot.','bot/','bot-');
$good_bots = array('Google','MSN','bing','Slurp','Yahoo','DuckDuck');
$banned_redirect_url = 'http://english-1329329990.spampoison.com';
// Visitor's IP address and Browser (User Agent)
$ip_address = $_SERVER['REMOTE_ADDR'];
$browser = $_SERVER['HTTP_USER_AGENT'];
// Declared Temporary Variables
$ipfound = $piece = $botfound = $gbotfound = $ubotfound = '';
// Checks for Banned IP Addresses and Bots
if($banned_redirect_url != ''){
// Checks for Banned IP Address
if(!empty($banned_ip_addresses)){
if(in_array($ip_address, $banned_ip_addresses)){$ipfound = 'found';}
if($ipfound != 'found'){
$ip_pieces = explode('.', $ip_address);
foreach ($ip_pieces as $value){
$piece = $piece.$value.'.';
if(in_array($piece, $banned_ip_addresses)){$ipfound = 'found'; break;}
}
}
if($ipfound == 'found'){header("location: $banned_redirect_url"); exit();}
}
// Checks for Banned Bots
if(!empty($banned_bots)){
foreach ($banned_bots as $bbvalue){
$pos1 = stripos($browser, $bbvalue);
if($pos1 !== false){$botfound = 'found'; break;}
}
if($botfound == 'found'){header("location: $banned_redirect_url"); exit();}
}
// Checks for Banned Unknown Bots
if(!empty($good_bots)){
foreach ($good_bots as $gbvalue){
$pos2 = stripos($browser, $gbvalue);
if($pos2 !== false){$gbotfound = 'found'; break;}
}
}
if($gbotfound != 'found'){
if(!empty($banned_unknown_bots)){
foreach ($banned_unknown_bots as $bubvalue){
$pos3 = stripos($browser, $bubvalue);
if($pos3 !== false){$ubotfound = 'found'; break;}
}
if($ubotfound == 'found'){header("location: $banned_redirect_url"); exit();}
}
}
}
// ---------------------------------------------------------------------------------------------------------------
?>
Cloudflare? Hoặc bạn có thể đánh giá một chuyển hướng dựa trên JS bị xáo trộn và giả mạo nó đến các bot đã biết bằng cách gửi cho họ một chuyển hướng tiêu đề thay thế. Nhưng người dùng thông thường phải có JS và có thể theo dõi chuyển hướng bị làm mờ. –
@Sandeep Kumar, nếu thực tế bạn vẫn đọc [bài đăng] (http://scraping.pro/my-site-is-being-scraped-how-can-i-prevent-being-scraped/). –