muduzhu

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

http://perl.about.com/od/cgiprogramminginperl/a/020905.htm

How to identify a browser

HTTP_USER_AGENT is one of the 19 CGI environment variables defined by The National Center for Supercomputing Applications (NCSA). It is used to identify the user agent that the client is using when a CGI script is accessed from a web page.

What is a user agent?

A user agent is the application a client uses to access a web page, which may be a browser, a mobile device or service such as AvantGo, or web crawlers, among other things. HTTP_USER_AGENT is the CGI environment variable that is generated to identify the user agent, and this information may be used to good effect if you want to present specific pages to your users based on what they're using to view the page. It's also handy if you want to use the robots.txt file to exclude a crawler from your site, or particular sections of your site.

What does the HTTP_USER_AGENT string look like?

Typically, the information included in HTTP_USER_AGENT includes the name of the client application and version, the operating system used by the host, and the language. Other information may also be included, such as a URL or email address.

Some of the strings identifying browsers are a little confusing, and this stems from the struggle for browser dominance. As one browser gains popularity, web sites may cater to it, or exclude other browsers entirely, even though they may handle complex pages being sent to it. Therefore, some browsers send a spoofed or cloaked string so that the page content will be retrieved properly.

For example, early versions of Internet Explorer sent an HTTP_USER_AGENT string identifying it as Mozilla, which was then used by Netscape Navigator. Now that Internet Explorer has gained dominance, some newer browsers follow in the same tradition, identifying themselves as Mozilla, MSIE, and the correct browser name.

Here are some examples 
=== 
Internet Explorer 
Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0) 
Mozilla/4.0 (compatible; MSIE 4.0 Crawler; Windows 95) 

Netscape Navigator 
Mozilla/4.0b1 (Win95; I) 
Mozilla/3.01Gold (Macintosh; U; PPC) 
===

How can I know the correct user agent?

The easiest way to have the correct user agent at your fingertips is to use a compiled list. A good one is presented by Zytrax, and Siteware Technologies.

posted on 2010-04-09 22:32  大黄牛  阅读(1296)  评论(0编辑  收藏  举报