Home >Backend Development >PHP Problem >How 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.
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['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) { return true; } elseif (isset($_SERVER['HTTP_ACCEPT']) && strpos(strtoupper($_SERVER['HTTP_ACCEPT']), "VND.WAP.WML")) { return true; } elseif (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) { return true; } elseif (isset($_SERVER['HTTP_USER_AGENT']) && preg_match('/(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',$_SERVER['HTTP_USER_AGENT'])) { 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('Location:https://baidu.com/'); }else{ header('Location:https://v.qq.com/'); } ?>
- 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!