Verify Googlebot with PHP

Webmasters maynot be pleased if the content they want to be available to googlebot only is available to any users when they switch their user-agent to Googlebot. But webmaster can easily trackdown this diguised visits.

Here is a simple function in php to verify if the visit claiming to be from Googlebot is a true Googlebot visit.

  1. <?php
  2. function googleBot()
  3. {
  4.  
  5. if(preg_match("/Googlebot/",$_SERVER[‘HTTP_USER_AGENT’]))
  6. {
  7. //robot ip address is assigned to $ipaddress
  8. $ipaddress = $_SERVER[‘REMOTE_ADDR’];
  9.  
  10. //hostname is assigned to $hostname
  11. $hostname = gethostbyaddr($ipaddress);
  12.  
  13. if(preg_match("/googlebot.com/",$hostname))
  14. {
  15. // returns true if googlebot.com is found in hostname
  16. return true;
  17. }
  18. }
  19. return false;
  20. }

This function returns boolean true or false and can be used anywhere in your program to show wanted behavior.

The article is based on Verifying Googlebot

One Comment

  1. Great info. Thanks for the info.

Leave a Reply