In a more intelligent program, PHP can obtain the client browser and operating system information, and then depending on the browser and system type, loading different pages to provide more personalized service.
Here we come together to learn how to use PHP to get client browser and operating system information, interested friends can refer to the next.
1, PHP Super global variable $_server array field [' Http_user_agent '] get access to all information of the user
The following is a string obtained by $_server[' Http_user_agent '):
mozilla/5.0 (Windows; U Windows NT 6.1; En-US) applewebkit/533.2 (khtml, like Gecko) chrome/5.0.342.3 safari/533.2
2, using regular expressions to match the above string, the user's browser, operating system information.
3, PHP according to the matching string (the user's browser operating system information), the allocation of the required CSS, JS, etc., returned to the user
My browser and operating system information:
Browser:chrome 5.0
Platform:windows 7
A complete example is given below.
<?php
Display the browser information of the Access user
Echo ' Browser: '. Determinebrowser ($Agent). ' <BR> ';
Show access to the user's operating system platform
Echo ' Platform: '. Determineplatform ($Agent). ' <BR> ';
Link:http://www.jbxue.com
A positive value expression that is better than parsing the string in $_server[' Http_user_agent '] gets access to the user's browser information
function Determinebrowser ($Agent) {
$browseragent = ""; Browser
$browserversion = ""; Version of the browser
if (Ereg (' MSIE ([0-9].[ 0-9]{1,2}) ', $Agent, $version)) {
$browserversion = $version [1];
$browseragent = "Internet Explorer";
} else if (Ereg (' opera/([0-9]{1,2}.[ 0-9]{1,2}) ', $Agent, $version)) {
$browserversion = $version [1];
$browseragent = "Opera";
} else if (Ereg (' firefox/([0-9.] {1,5}) ', $Agent, $version)} {
$browserversion = $version [1];
$browseragent = "Firefox";
}else if (Ereg (' chrome/([0-9.] {1,3}) ', $Agent, $version)} {
$browserversion = $version [1];
$browseragent = "Chrome";
}
else if (Ereg (' safari/([0-9.] {1,3}) ', $Agent, $version)} {
$browseragent = "Safari";
$browserversion = "";
}
else {
$browserversion = "";
$browseragent = "Unknown";
}
return $browseragent. " ". $browserversion;
}
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.