


Function code to use PHP to determine browser type and language_PHP tutorial
We often see that some websites will display the browser type and language you are currently using. For example, it displays: The browser you are using is IE6, Traditional Chinese. Doesn’t it look cool?
In fact, such a function is not difficult to implement. It is nothing more than judging the type and language of the browser. It should be very simple if you use JS. Here we look at how to use PHP to implement such a function. Since we are making a judgment, we You can use PHP's conditional statement if..else to determine the implementation.
Determine browser type:
Determine browser language:
The specific procedure to determine the browser type is as follows:
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"];
Copy code
The code is as follows:
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE' ], 0, 4); //Only take the first 4 digits, so that only the highest priority language is judged. If you take the first 5 digits, en and zh may occur, which affects the judgment. if (preg_match("/zh-c/i", $lang)) echo "Simplified Chinese"; else if (preg_match("/zh/i", $lang)) echo "Traditional Chinese";
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 "Japanese";
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: Determining the browser type mainly relies on analyzing the content of _SERVER["HTTP_USER_AGENT"], while analyzing the browser language is analyzing _SERVER["HTTP_ACCEPT_LANGUAGE"].
Principle: Because the browser will first send some content containing its own information (browser type, language) when connecting to the server. So here we mainly analyze _SERVER["HTTP_USER_AGENT"] (browser type) and _SERVER["HTTP_ACCEPT_LANGUAGE"] (browser language). All we have to do is read out these contents, and then use strpos or preg_match function to compare them. According to the design of the program, we can also design the page to be more beautiful.
http: //www.bkjia.com/PHPjc/326562.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In the cryptocurrency market, choosing a reliable trading platform is crucial. As a world-renowned digital asset exchange, the OK trading platform has attracted a large number of novice users in mainland China. This guide will introduce in detail how to register and use it on the OK trading platform to help novice users get started quickly.

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

Binance is one of the world's leading cryptocurrency trading platforms, providing trading services for a variety of digital assets. If you are considering using Binance for cryptocurrency trading, this article will provide you with a detailed installation and registration guide.

As the world's leading cryptocurrency trading platform, OKX provides users with diversified trading services and a secure trading environment. Since its establishment, OKX has attracted a large number of users and investors with its strong technical support and rich trading products. Whether you are a novice or a professional trader, OKX can meet your trading needs.

Go uses the "strings" package for string operations. 1) Use strings.Join function to splice strings. 2) Use the strings.Contains function to find substrings. 3) Use the strings.Replace function to replace strings. These functions are efficient and easy to use and are suitable for various string processing tasks.

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.
