User-agent in HTTP requests a variety of ways to determine the browser type the web crawler's request flag

Source: Internet
Author: User

When the user sends an HTTP request, the version information that is browsed is also included in the HTTP request message:

As shown, requesting a Google Plus request header contains the user's browser information:

user-agent:mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/27.0.1453.94 safari/537.36

We can get the client's browser information through the relevant API provided by the server-side language, and then return the different HTML documents to different browsers, so that we can return the beautiful display page for the modern browser.

In JavaScript, we also provide the relevant API to get information about the current browser:
Navigator.useragent

UserAgent provides the information that the browser User-agent in the HTTP request header that will be sent to the server side. After obtaining this information we can get the browser and version information via regular match:

Get useragent information sent by the browser   var useragentinfo = navigator.userAgent.toLowerCase ();d ocument.write (Useragentinfo + ' <br/> ');/** * Output running Browser information: * chrome:mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chro me/27.0.1453.94 safari/537.36 * ie10:mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) Chrome/27 .0.1453.94 safari/537.36 *///Gets the browse type and version through regular matching//For example, the Var agent that can get ie in this way = {};if (window). ActiveXObject)    agent.ie = Useragentinfo.match (/msie ([\d.] +)/) [1];//below for output information if (agent.ie)    document.write (agent.ie);  IE browser output Browse under version number, other browsers do not output information
The associated APIs are also available in PHP:
The Strpos () function returns the position of the first occurrence of a string in another string. If the string is not found, false is returned.
There are many server variables in $_server, where $_server[' Http_user_agent ' #当前请求的 user_agent: The contents of the head.

You can judge the requestor's browser and version as follows, note that the version here can be spoofed by the visitor and not necessarily correct.

<?php    if (Strpos ($_server["Http_user_agent"], "MSIE8.0"))        echo "IE8.0";    ElseIf (Strpos ($_server["Http_user_agent"], "MSIE7.0"))        echo "IE7.0";    ElseIf (Strpos ($_server["Http_user_agent"], "MSIE6.0"))        echo "IE6.0";    ElseIf (Strpos ($_server["Http_user_agent"], "FIREFOX/17"))        echo "Firefox17";    ElseIf (Strpos ($_server["Http_user_agent"], "FIREFOX/16"))        echo "Firefox16";    ElseIf (Strpos ($_server["Http_user_agent"], "Chrome"))        echo "Chrome";    ElseIf (Strpos ($_server["Http_user_agent"], "Safari"))        echo "Safari";    ElseIf (Strpos ($_server["Http_user_agent"], "opera"))        echo "opera";    else echo$_server["Http_user_agent"];? >

Conditional comments are initially present in Microsoft's Internet Explorer 5 browser and are supported until Internet Explorer 9. [1] Microsoft has announced that it will stop support in Internet Explorer 10 in the standard mode of processing pages-such as HTML5-but that legacy Web pages use this technology (in Compatibility View) to continue to work.

<!--[if! ie]><!--> In addition to IE can be recognized <!--<! [endif]-->
<!--[if ie]> all IE recognizable <! [endif]-->
<!--[if IE 6]> only IE6 recognizable <! [endif]-->
<!--[if Lt IE 6]> IE6 The following versions are recognized <! [endif]-->
<!--[if GTE IE 6]> IE6 and IE6 above are recognized <! [endif]-->
<!--[if IE 7]> only IE7 recognizable <! [endif]-->
<!--[if Lt IE 7]> IE7 The following versions are recognized <! [endif]-->
<!--[if GTE IE 7]> IE7 and IE7 above are recognized <! [endif]-->
<!--[if IE 8]> only IE8 recognizable <! [endif]--><!--[If IE 9]> only IE9 recognizable <! [endif]-->
Crawling Problem of web crawler

User-agent in HTTP requests a variety of ways to determine the browser type the web crawler's request flag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.