0

Check if IP belongs to Googlebot

Function PHP 1 revisions 234 2 years ago 2 years ago
/**
 * Checks whether an IP belongs to Google's crawling bot
 *
 * @param string $ip The IP address of the client
 * @return bool Whether the client is a Google bot
 */
private function isGoogleBot($ip) {
	$host = gethostbyaddr($ip);
	if (!ends_with($host, 'google.com') && !ends_with($host, 'googlebot.com'))
		return false;
	$records = dns_get_record($host);

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

	return false;
}

This function runs an rDNS and then a DNS in order to check if the client IP belongs to Googlebot, the recommended way by Google.