0

I got the following function while googling for bot behaviour. while i am using this code, i get error for eregi() expression. i am not an expert of regualar expression. since ereri( ) has been depreciated, i am getting the same error.

 function check_if_spider()
        {
            // Add as many spiders you want in this array
            $spiders = array('Googlebot', 'Yammybot', 'Openbot', 'Yahoo', 'Slurp', 'msnbot', 'ia_archiver', 'Lycos', 'Scooter', 'AltaVista', 'Teoma', 'Gigabot', 'Googlebot-Mobile');

            // Loop through each spider and check if it appears in
            // the User Agent
            foreach ($spiders as $spider)
            {
                if (eregi($spider, $_SERVER['HTTP_USER_AGENT']))
                {
                    return TRUE;
                }
            }
            return FALSE;
        }

how can i modify the code to make it workin? google search says it should be converted to preg_match(); being a newbee, i am at failure, although i tried at my end..can somebody guide me?

Zaffar Saffee
  • 6,167
  • 5
  • 39
  • 77

2 Answers2

4

You aren't actually using regular expressions (you're matching literal strings) so there's no need to use preg_match().

Replace

if (eregi($spider, $_SERVER['HTTP_USER_AGENT']))

with

if (strpos($spider, $_SERVER['HTTP_USER_AGENT']) !== FALSE)
salathe
  • 51,324
  • 12
  • 104
  • 132
0

You can use the function strstr to compare strings. It returns false if the string in the first parameter does not contain the string in the second parameter.

foreach ($spiders as $spider)
    {
        if(strstr($_SERVER['HTTP_USER_AGENT'], $spider))
        {
            return TRUE;
        }
    }
lukassteiner
  • 787
  • 1
  • 6
  • 25