Home Backend Development PHP Tutorial Function code to use PHP to determine browser type and language_PHP tutorial

Function code to use PHP to determine browser type and language_PHP tutorial

Jul 21, 2016 pm 03:13 PM
php code use function judgment and us Browser of at present type website language

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:

Copy the code The code 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("/en/i", $lang))
echo "English";
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

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/326562.html

We often see some websites that display the type of browser you are currently using and the language you are using, such as What is displayed is: The browser you are using is IE6, traditional Chinese characters. It doesn’t look like...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1676
14
PHP Tutorial
1278
29
C# Tutorial
1257
24
How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China How to register in the ok exchange in China? ok trading platform registration and use guide for beginners in mainland China May 08, 2025 pm 10:51 PM

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.

PHP Performance Optimization Checklist: Improve Speed Now PHP Performance Optimization Checklist: Improve Speed Now May 12, 2025 am 12:07 AM

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

PHP performance optimization strategies. PHP performance optimization strategies. May 13, 2025 am 12:06 AM

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

Detailed Guide to Installation and Registration of Binance Binance Exchange (2025 Latest Steps) Detailed Guide to Installation and Registration of Binance Binance Exchange (2025 Latest Steps) May 08, 2025 pm 11:06 PM

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.

okx exchange portal okx trading platform official website login portal okx exchange portal okx trading platform official website login portal May 08, 2025 pm 11:15 PM

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: String Manipulation with the Standard 'strings' Package Go: String Manipulation with the Standard 'strings' Package May 09, 2025 am 12:07 AM

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.

PHP Email: Step-by-Step Sending Guide PHP Email: Step-by-Step Sending Guide May 09, 2025 am 12:14 AM

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

PHP Dependency Injection: Improve Code Testability PHP Dependency Injection: Improve Code Testability May 12, 2025 am 12:03 AM

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.

See all articles