Do action based on user agent

Discussion in 'PHP' started by agnivo007, May 18, 2007.

  1. #1
    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
     
    agnivo007, May 18, 2007 IP
  2. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #2
    php.net/in_array
     
    wing, May 18, 2007 IP
  3. agnivo007

    agnivo007 Peon

    Messages:
    4,290
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    agnivo007, May 18, 2007 IP
  4. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #4
    $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, May 18, 2007 IP
  5. DeViAnThans3

    DeViAnThans3 Peon

    Messages:
    785
    Likes Received:
    83
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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 ;)
     
    DeViAnThans3, May 19, 2007 IP
  6. agnivo007

    agnivo007 Peon

    Messages:
    4,290
    Likes Received:
    289
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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:
     
    agnivo007, May 19, 2007 IP
  7. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #7
    	$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:
    :)
     
    wing, May 19, 2007 IP
  8. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #8
    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:
    :)
     
    wing, May 20, 2007 IP