Home >Backend Development >PHP Problem >How to determine whether php is mobile or pc

How to determine whether php is mobile or pc

藏色散人
藏色散人Original
2022-11-09 09:12:372686browse

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