search
HomeBackend DevelopmentPHP ProblemHow to determine whether php is mobile or pc

php method to determine whether it is mobile or PC: 1. Create a php sample file; 2. Define the function as "function is_Mobile(){if (isset($_SERVER['HTTP_VIA']) && stristr( $_SERVER['HTTP_VIA'], "wap")) {return true;}..."; 3. Just call the function.

How to determine whether php is mobile or pc

The operating environment of this tutorial: Windows 7 system, PHP version 8.1, Dell G3 computer.

How to determine whether PHP is mobile or PC?

php determines whether the user is PC or mobile

Code implementation

1. Define function

<?php 
    function is_Mobile()
    {
        if (isset($_SERVER[&#39;HTTP_VIA&#39;]) && stristr($_SERVER[&#39;HTTP_VIA&#39;], "wap")) {
            return true;
        } elseif (isset($_SERVER[&#39;HTTP_ACCEPT&#39;]) && strpos(strtoupper($_SERVER[&#39;HTTP_ACCEPT&#39;]), "VND.WAP.WML")) {
            return true;
        } elseif (isset($_SERVER[&#39;HTTP_X_WAP_PROFILE&#39;]) || isset($_SERVER[&#39;HTTP_PROFILE&#39;])) {
            return true;
        } elseif (isset($_SERVER[&#39;HTTP_USER_AGENT&#39;]) && preg_match(&#39;/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera |Googlebot-Mobile|YahooSeeker\/M1A1-R2D2|android|iphone|ipod|mobi|palm|palmos|pocket|portalmmm|ppc;|smartphone|sonyericsson|sqh|spv|symbian|treo|up.browser|up.link|vodafone|windows ce|xda |xda_)/i&#39;,$_SERVER[&#39;HTTP_USER_AGENT&#39;])) {
            return true;
        } else {
            return false;
        }
    }?>

2 . Function call

- Called in the same file

Called when it is necessary to determine whether the user is a mobile or PC client

<?php if (is_Mobile()) {
   header(&#39;Location:https://baidu.com/&#39;);
}else{
   header(&#39;Location:https://v.qq.com/&#39;);
}
?>

- Called in different files

At the beginning, use "require_once()" to reference the PHP code module containing the function in 1, and call it when you need to determine whether the user is a mobile terminal or a PC terminal.

Recommended learning: "PHP Video Tutorial

The above is the detailed content of How to determine whether php is mobile or pc. For more information, please follow other related articles on the PHP Chinese website!

Statement
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

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools