Rumah  >  Artikel  >  pembangunan bahagian belakang  >  php怎么判断移动端还是pc端

php怎么判断移动端还是pc端

藏色散人
藏色散人asal
2022-11-09 09:12:372583semak imbas

php判断移动端还是pc端的实现方法:1、创建一个php示例文件;2、定义函数为“function is_Mobile(){if (isset($_SERVER['HTTP_VIA']) && stristr($_SERVER['HTTP_VIA'], "wap")) {return true;}...”;3、调用函数即可。

php怎么判断移动端还是pc端

本教程操作环境:windows7系统、PHP8.1版、Dell G3电脑。

php怎么判断移动端还是pc端?

php判断用户是PC端还是移动端

代码实现

1. 定义函数

<?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. 函数调用

- 同一文件中调用

在需要判断用户是移动端还是PC端处调用

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

- 不同文件中调用

在开头处通过"require_once()"引用包含1中函数的php代码模块,并在需要判断用户是移动端还是PC端处调用

推荐学习:《PHP视频教程

Atas ialah kandungan terperinci php怎么判断移动端还是pc端. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Artikel sebelumnya:php怎么禁止pc访问Artikel seterusnya:php用不了user怎么办