Home Backend Development PHP Problem Determine whether the current browser is WeChat browser or mobile terminal

Determine whether the current browser is WeChat browser or mobile terminal

Jun 29, 2021 pm 03:18 PM
Mobile terminal

Sometimes the project needs to judge the browser and execute different logic for different browsers. You can use JS code to determine whether the browser is a WeChat browser, and use PHP code to determine whether the browser is mobile.

Determine whether the current browser is WeChat browser or mobile terminal

#Sometimes the project needs to judge the browser and execute different logic for different browsers.

1, use JS code to determine whether the browser is a WeChat browser

function is_weixn(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        return true;
    } else {
        return false;
    }
}
Copy after login

2, use PHP code to determine whether the browser is a mobile terminal

function isMobile()
			{ 
				// 如果有HTTP_X_WAP_PROFILE则一定是移动设备
				if (isset ($_SERVER['HTTP_X_WAP_PROFILE']))
				{
					return "1";
				} 
				// 如果via信息含有wap则一定是移动设备
				if (isset ($_SERVER['HTTP_VIA']))
				{ 
					// 找不到为flase,否则为true
					return stristr($_SERVER['HTTP_VIA'], "wap") ? "1" : "0";
				} 
				// 脑残法,判断手机发送的客户端标志,兼容性有待提高
				if (isset ($_SERVER['HTTP_USER_AGENT']))
				{
					$clientkeywords = array ('nokia',
						'sony',
						'ericsson',
						'mot',
						'samsung',
						'htc',
						'sgh',
						'lg',
						'sharp',
						'sie-',
						'philips',
						'panasonic',
						'alcatel',
						'lenovo',
						'iphone',
						'ipod',
						'blackberry',
						'meizu',
						'android',
						'netfront',
						'symbian',
						'ucweb',
						'windowsce',
						'palm',
						'operamini',
						'operamobi',
						'openwave',
						'nexusone',
						'cldc',
						'midp',
						'wap',
						'mobile'
						); 
					// 从HTTP_USER_AGENT中查找手机浏览器的关键字
					if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT'])))
					{
						return "1";
					} 
				} 
				// 协议法,因为有可能不准确,放到最后判断
				if (isset ($_SERVER['HTTP_ACCEPT']))
				{ 
					// 如果只支持wml并且不支持html那一定是移动设备
					// 如果支持wml和html但是wml在html之前则是移动设备
					if ((strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) !== false) && (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;) === false || (strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;vnd.wap.wml&#39;) < strpos($_SERVER[&#39;HTTP_ACCEPT&#39;], &#39;text/html&#39;))))
					{
						return "1";
					} 
				} 
				return "0";
			}
		
		$ismobile=isMobile();//判断是否移动端
		if ($ismobile=="1"){
			echo "浏览器为移动端";
			
		}
		
		if ($ismobile=="0"){
			 echo "浏览器不为移动端";
		
		}
Copy after login

The above two pieces of code, brother All tested, true and effective.

Recommended learning: php video tutorial

The above is the detailed content of Determine whether the current browser is WeChat browser or mobile terminal. For more information, please follow other related articles on the PHP Chinese website!

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 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
1664
14
PHP Tutorial
1268
29
C# Tutorial
1244
24
How to use mobile gesture operations in Vue projects How to use mobile gesture operations in Vue projects Oct 08, 2023 pm 07:33 PM

How to use mobile gesture operations in Vue projects With the popularity of mobile devices, more and more applications need to provide a more friendly interactive experience on the mobile terminal. Gesture operation is one of the common interaction methods on mobile devices, which allows users to complete various operations by touching the screen, such as sliding, zooming, etc. In the Vue project, we can implement mobile gesture operations through third-party libraries. The following will introduce how to use gesture operations in the Vue project and provide specific code examples. First, we need to introduce a special

Solve the problem of multi-touch points on Vue mobile terminal Solve the problem of multi-touch points on Vue mobile terminal Jun 30, 2023 pm 01:06 PM

In mobile development, we often encounter the problem of multi-finger touch. When users use multiple fingers to swipe or zoom the screen on a mobile device, how to accurately recognize and respond to these gestures is an important development challenge. In Vue development, we can take some measures to solve the problem of multi-finger touch on the mobile terminal. 1. Use the vue-touch plug-in vue-touch is a gesture plug-in for Vue, which can easily handle multi-finger touch events on the mobile side. We can install vue-to via npm

How to solve the double-click amplification problem on mobile terminals in Vue development How to solve the double-click amplification problem on mobile terminals in Vue development Jun 29, 2023 am 11:06 AM

With the popularity of mobile devices, using Vue for mobile development has become a common choice. However, we often face a problem during mobile development, which is double-clicking to zoom in. This article will focus on this problem and discuss how to solve the specific method of double-click amplification on the mobile terminal in Vue development. The double-click enlargement problem on mobile devices occurs mainly because the mobile device automatically enlarges the zoom ratio of the web page when double-clicking on the touch screen. For general web development, this kind of double-click to enlarge is usually beneficial because it can

A complete guide to implementing mobile responsive layout in Vue (Vant) A complete guide to implementing mobile responsive layout in Vue (Vant) Jun 09, 2023 pm 04:09 PM

A Complete Guide to Implementing Mobile Responsive Layout in Vue (Vant) Mobile responsive layout is a very important part of modern web development. With the popularity of mobile devices, how to quickly respond to the size and resolution of the user's mobile phone screen has become a One of the challenges front-end engineers have to face. The Vue framework comes with responsive layout features, and there are also many third-party libraries to help us implement responsive layout. Among them, Vant component library is a Vue mobile UI library because it is very powerful, easy to use and customized, and is fully compatible with mobile devices.

How to implement mobile map positioning function using Python and Baidu Map API How to implement mobile map positioning function using Python and Baidu Map API Jul 29, 2023 pm 11:33 PM

Method of implementing mobile map positioning function using Python and Baidu Map API. With the development of mobile Internet, map positioning function has become more and more common in mobile applications. Python, as a popular programming language, can also implement mobile map positioning functions by using Baidu Map API. The following will introduce the steps to implement the map positioning function using Python and Baidu Map API, and provide corresponding code examples. Step 1: Apply for Baidu Map API Key Before starting, we first need to apply

Vue development: Optimizing the stuck problem of gesture scaling on the mobile terminal Vue development: Optimizing the stuck problem of gesture scaling on the mobile terminal Jun 30, 2023 pm 04:33 PM

How to solve the stuck problem of mobile gesture zooming pages in Vue development. In recent years, the popularity of mobile applications has made gesture operations an important way of user interaction. In Vue development, implementing the gesture zoom function on the mobile terminal often encounters the problem of page lag. This article will explore how to solve this problem and provide some optimization strategies. Understand the principle of gesture scaling. Before solving the problem, we first need to understand the principle of gesture scaling. Gesture zooming is implemented by listening to touch events. When the user slides the screen with two fingers, the page will follow the sliding movement of the fingers.

How to handle mobile and responsive design in PHP forms How to handle mobile and responsive design in PHP forms Aug 10, 2023 am 11:51 AM

How to deal with mobile and responsive design in PHP forms. With the popularity and frequency of mobile devices increasing, and more and more users using mobile devices to access websites, adapting to mobile has become an important issue. When dealing with PHP forms, we need to consider how to achieve a mobile-friendly interface and responsive design. This article explains how to handle mobile and responsive design in PHP forms and provides code examples. 1. Responsive forms using HTML5 HTML5 provides some new features that can easily implement responsive forms.

Using Vant in Vue to implement the mobile wizard introduction page effect Using Vant in Vue to implement the mobile wizard introduction page effect Jun 09, 2023 pm 04:10 PM

With the continuous development of the mobile Internet, more and more companies are beginning to use mobile platforms to display their products or services to users. When showing products or services to users, the wizard introduction page has become an important part of the display form. Vue.js is a popular JavaScript framework, and Vant is an excellent mobile component library based on Vue.js, which can help us quickly build mobile applications. This article will introduce how to use Vue.js and Vant to create a mobile wizard introduction

See all articles