开发者

How does PHP access information about the client's browser?

开发者 https://www.devze.com 2023-03-02 21:17 出处:网络
How is it possible for client browser data to be saved in an array in PHP? PHP runs on the server side, so I don\'t understand how i开发者_运维百科t has access to information about the client\'s brow

How is it possible for client browser data to be saved in an array in PHP?

PHP runs on the server side, so I don't understand how i开发者_运维百科t has access to information about the client's browser.


User agent data is usually sent with every HTTP requests, in the User-Agent HTTP header field. You might want to read up on HTTP message formats in general. For example, this is part of the HTTP request that my browser sent to load jQuery on this very page:

GET http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js HTTP/1.1
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Connection: keep-alive
If-Modified-Since: Fri, 01 Apr 2011 21:23:55 GMT
Accept-Charset: UTF-8,*;q=0.5
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.60 Safari/534.24
Accept: */*

PHP reads the client browser data that you're asking about from the User-Agent header field.


The client sends data to the server which the server uses to build the array (I'm assuming you're talking about $_GET, $_POST, $_SERVER, etc.)


You will find it here

$_SERVER['HTTP_USER_AGENT']

You may need to parse this by regex to get the browser name and version separately.


$_REQUEST

An associative array that by default contains the contents of $_GET, $_POST and $_COOKIE.


The data is submited by the browser when a new page is requested, PHP just puts it into an array for your convenience.

You should start by reading a bit about HTTP (GET and POST to begin with), and HTTP headers.

0

精彩评论

暂无评论...
验证码 换一张
取 消