How to tell if the visitor is Google bot, or Facebook bot [duplicate]

0

Is there a way, via PHP, to know that who is visiting my page is Google bot and Facebook bot?

    
asked by anonymous 15.03.2016 / 03:35

1 answer

1

Google

if (strstr(strtolower($_SERVER['HTTP_USER_AGENT']), "googlebot")) {
    // Provavelmente proveio do google.
    // Aqui você implementa as suas firulas.
}

Facebook:

if (
    strpos($_SERVER["HTTP_USER_AGENT"], "facebookexternalhit/") !== false ||          
    strpos($_SERVER["HTTP_USER_AGENT"], "Facebot") !== false
) {
    // Provavelmente proveio do facebook.
    // Aqui você implementa as suas firulas.
}

Note: Be aware that HTTP_USER_AGENT is easy to manipulate. Anyone can create a script that identifies itself as googlebot , for example. So do not trust 100% in this parameter.

To ensure greater integrity you will have to do reverse DNS checks, IP source, etc.

    
15.03.2016 / 03:53