intenseone345 Posted January 23, 2014 Share Posted January 23, 2014 (edited) Hello, how can i add a function call within this php script to echo the result as either "the bot is" or "no bots found" im not sure on how to code it and make use of this script? Thanks <?php protected function isBot() { /* This function will check whether the visitor is a search engine robot */ $bots = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi", "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz", "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot", "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot", "Butterfly","Twitturls","Me.dium","Twiceler"); foreach($bots as $bot) { if(strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) return true; } return false; } ?> Edited January 23, 2014 by intenseone345 Quote Link to comment Share on other sites More sharing options...
requinix Posted January 23, 2014 Share Posted January 23, 2014 The function, technically a class method, returns a boolean true or false depending whether it found one of those bot strings. Call the function and output whatever message is appropriate. Returning values from functions if/else if Quote Link to comment Share on other sites More sharing options...
intenseone345 Posted January 23, 2014 Author Share Posted January 23, 2014 Im thinking maybe like this but it wont return the name of the bot done this way. Also would anyone know how to test a script like this making your own own browser look like a bot, i tried adding "Mozilla" in the array but it did not work? <?php protected function isBot() { /* This function will check whether the visitor is a search engine robot */ $bots = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi", "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz", "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot", "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot", "Butterfly","Twitturls","Me.dium","Twiceler","MSIE","Microsoft Internet Explorer","Mozilla/4.0"); foreach($bots as $bot) { if(strpos($_SERVER['HTTP_USER_AGENT'], $bot) !== false) return true; } return false; } if (!isBot()) { echo "human"; } if (is_bot()) { echo "bot"; } ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted January 23, 2014 Share Posted January 23, 2014 For the record, are you posting your exact code? Because if you are then it isn't executing in the first place because it's not valid syntax. Plus, is_bot() does not (as far as you've posted) exist so that won't work either. But to answer the question, you can make the function return not true if it finds the string, but the string it found. It can still return false if not. Then, when you call the function, you assign the return value to a variable and if it's not empty then it'll have the bot name. And one more thing: MSIE, "Microsoft Internet Explorer", and "Mozilla/4.0" are not bots. Quote Link to comment Share on other sites More sharing options...
cyberRobot Posted January 23, 2014 Share Posted January 23, 2014 Plus, is_bot() does not (as far as you've posted) exist so that won't work either. Assuming that the is_bot() call is supposed to be isBot(), you could just use "else" instead of running the function twice. if(isBot()) { echo 'bot'; } else { echo 'human'; } Quote Link to comment Share on other sites More sharing options...
intenseone345 Posted January 24, 2014 Author Share Posted January 24, 2014 Ok why is this not valid syntax? thanks for your help <?php public function isBot() { /* This function will check whether the visitor is a search engine robot */ $bots = array("Teoma", "alexa", "froogle", "Gigabot", "inktomi", "looksmart", "URL_Spider_SQL", "Firefly", "NationalDirectory", "Ask Jeeves", "TECNOSEEK", "InfoSeek", "WebFindBot", "girafabot", "crawler", "www.galaxy.com", "Googlebot", "Scooter", "Slurp", "msnbot", "appie", "FAST", "WebBug", "Spade", "ZyBorg", "rabaz", "Baiduspider", "Feedfetcher-Google", "TechnoratiSnoop", "Rankivabot", "Mediapartners-Google", "Sogou web spider", "WebAlta Crawler","TweetmemeBot", "Butterfly","Twitturls","Me.dium","Twiceler","Yahoo","Bling","bingbot","ezooms","Baidu"); foreach($bots as $bot) { if(strpos, strtolower($_SERVER['HTTP_USER_AGENT'], $bot) !== false) return true; } return false; } if(isBot()) { echo ucfirst($bot) . ' '; } else { echo 'no bots'; } ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted January 24, 2014 Share Posted January 24, 2014 Your latest code? Because you can't make a function "public" unless it's inside a class. Because you can't use strpos as just some name floating out in the middle of nowhere. Because you can't do multiple conditions in an if like that. Because strtolower() only has one parameter. Because strtolower() returns a string, not a boolean. Because if you lowercase the user agent string then you'll never find any bot names with capital letters. Because you can't use $bot outside of the function where it was defined. I have some bad news: you have no clue what you're doing. If you want the code to be changed then you should find someone to do it for you. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.