0

Check if IP belongs to Bing's bot

Function PHP 1 revisions 104 a year ago a year ago
/**
 * Checks whether an IP belongs to Bing's crawling bot
 *
 * @param string $ip The IP address of the client
 * @return bool Whether the client is Bing's bot
 */
private function isBingBot($ip) {
	$host = gethostbyaddr($ip);
	if (!ends_with($host, 'search.msn.com'))
		return false;
	$records = dns_get_record($host);

	foreach($records as $record)
		if ($record['ip'] === $ip)
			return true;

	return false;
}

This function performs a rDNS lookup and then a DNS lookup in order to check if the client IP belongs to Bing's crawling bot, the recommended way by Bing.