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

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function