SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I'm using the $HTTP_USER_AGENT variable on my page to detect the user's browser. It returns something like this:

    Mozilla/4.0 (compatible; MSIE 4.01; Windows 98; Compaq)

    I'm trying to use a simple if statement to display a block of code if the user is using Netscape, and another if the user is using IE - however given the complex nature of that block of text in bold, I'm not sure where to go from here.

    All help is appreciated.

  2. #2
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this (blatantly pinched from the PHP Manual I might add):

    Code:
    <?php
    function list_array ($array) {
        while (list ($key, $value) = each ($array)) {
        $str .= "<b>$key:</b> $value<br>\n";
        }
        return $str;
    }
    echo "$HTTP_USER_AGENT<hr>\n";
    $browser = get_browser();
    echo list_array ((array) $browser);
    ?>
    The core bit is get_browser(), this will only work though if your host has installed the browscap.ini file, it works in PHP3 and PHP4 (I think it was broken in PHP4 until 4.0.3pl1).

    If it doesn't work then you can try this:

    Code:
    	if ( eregi( "MSIE ([[:digit:]]*\.[[:alnum:]]*)", $HTTP_USER_AGENT, $aRegs ) ) {
    	
    		$agent[name] = "ie";
    		$agent[version] = $aRegs[1];
    	
    	} elseif ( eregi ( "Mozilla/([[:digit:]]*\.[[:digit:]][[:alnum:]]*).*Gecko", $HTTP_USER_AGENT, $aRegs ) ) {
    
    		$agent[name] = "moz";
    		$agent[version] = $aRegs[1];		
    
    	} elseif ( eregi ( "Mozilla/([[:digit:]]*\.[[:digit:]][[:alnum:]]*).*\(.*; [IU]", $HTTP_USER_AGENT, $aRegs ) ) {
    	
    		$agent[name] = "nav";
    		$agent[version] = $aRegs[1];
    	
    	} elseif ( eregi ( "Opera.([[:digit:]]*\.[[:alnum:]]*)", $HTTP_USER_AGENT, $aRegs ) ) {
    	
    		$agent[name] = "opera";
    		$agent[version] = $aRegs[1];	
    	
    	}
    Hope this helps you out. If you want the code by email (cos vB has probably trashed it) then let me know.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  3. #3
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Email would be great: chris@mycoding.com - I admit this is above my level of programming - does the block of code I mentioned simply go inside each if/elseif statement?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •