Sometimes we need to make the appropriate output by getting the type of browser and the language used. So how can you get the type of browser and the language you use? This article describes how to use PHP to obtain the type of browser and the language used.
Using PHP to determine the browser type is actually very simple. The browser will send some content (browser type, language, etc.) that contains its own information when it connects with the server.
Here our main analysis is _server "http_user_agent" and _server "Http_accept_language". All we have to do is read it out and then use the Strpos or Preg_match function to compare it.
Header ("Content-type:text/html;charset=utf-8"); if (Strpos ($_server["Http_user_agent"], "MSIE 8.0")) { echo "Internet Explorer 8.0"; } else if (Strpos ($_server["Http_user_agent"], "MSIE 7.0")) { echo "Internet Explorer 7.0"; } else if (Strpos ($_server["Http_user_agent"], "MSIE 6.0")) { echo "Internet Explorer 6.0"; } else if (Strpos ($_server["Http_user_agent"], "FIREFOX/3")) { echo "Firefox 3"; } else if (Strpos ($_server["Http_user_agent"], "FIREFOX/2")) { echo "Firefox 2"; } else if (Strpos ($_server["Http_user_agent"], "chrome") { echo "Google Chrome"; } else if (Strpos ($_server["Http_user_agent"], "Safari") { echo "Safari"; } else if (Strpos ($_server["Http_user_agent"], "opera")) { echo "opera"; } else{ echo $_server["Http_user_agent"]; }
The specific procedures for judging the browser language are as follows:
$lang =substr ($_server["Http_accept_language"],0,4); if (Preg_match ("/zh-c/i", $lang)) { echo "Simplified Chinese"; } else if (Preg_match ("/zh/i", $lang)) { echo "Traditional Chinese"; } else if (Preg_match ("/en/i", $lang)) { echo "中文版"; } else if (Preg_match ("/fr/i", $lang)) { echo "French"; } else if (Preg_match ("/de/i", $lang)) { echo "German"; } else if (Preg_match ("/jp/i", $lang)) { echo "Japanse"; } else if (Preg_match ("/ko/i", $lang)) { echo "Korean"; } else if (Preg_match ("/es/i", $lang)) { echo "Spanish"; } else if (Preg_match ("/sv/i", $lang)) { echo "Swedish"; } else{ echo $_server["Http_accept_language"]; }
Summary: To determine the browser type is mainly by analyzing the content of _server["Http_user_agent", while the analysis of the browser language is the analysis _server["Http_accept_language".