If user_agent does not contain any of the words from an array of words I define, then a certain action is taken. Please help me code this block in php. Code should be as small as possible and streamlined. Thanks
aha! that I know...I need a complete block of php code or function. Say I define array $a = 'opera','firefox','bot' $_SERVER['HTTP_USER_AGENT'] - I get the browser or robot's user_agent string. Now, if any of the words in $a does not occur in user_agent string, I do something. Remember user_agent returns a complex phrase with numbers, symbols and letters.
$array = array('MSIE 6', 'Opera/6', 'Mozilla/6', 'Googlebot'); foreach($array as $key => $value) { if( in_array( !strstr($_SERVER['HTTP_USER_AGENT'], $value), $array ) ) { // replace this with your actions echo "Key: $key => Value: $value"; } } PHP: ...does the job.
wing's code will do the job indeed. What you might also find interesting, is the get_browser() function; note that not all hosts support this! However, if your host supports it, this can do the job very well too
I get the following output : What need is like this (the code is incorrect, kindly help me correct this and test run it) <?php $array = array('firefox','bot','slurp','inktomi'); $ua_flag=1; //assume non specified user agent foreach($array as $key => $value) { if ( in_array( !stristr($_SERVER['HTTP_USER_AGENT'], $value), $array ) ) { $ua_flag=0; //specified user agent found break; } } echo $_SERVER['HTTP_USER_AGENT']; if ($ua_flag==1) echo "\nspecified agents not detected"; ?> PHP:
$ua_flag=""; $array = array('firefox','bot','slurp','inktomi'); foreach($array as $key => $value) { if( in_array( !strstr($_SERVER['HTTP_USER_AGENT'], $value), $array ) ) { $ua_flag = 0; //--> No value was found, set $ua_flag to 0 } else { $ua_flag = 1; //--> A value was found, set $ua_flag to 1 } } PHP:
Same thing wrapped as a function. Returns 1 (true), if value is found. $myarray = array('firefox','bot','slurp','inktomi'); function getstuff() { global $myarray; foreach($myarray as $key => $value) { if( in_array( !strstr($_SERVER['HTTP_USER_AGENT'], $value), $myarray ) ) { return false; } else { return true; } } } PHP: