PHP怎样识别手机与电脑的不同访问

发布于 2022-08-24 10:48:03 字数 52 浏览 15 评论 0

像很多网站一样,访问时会识别为手机版或桌面版。
我想应该是用脚本语言实现的吧。

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

旧街凉风 2022-08-31 10:48:03

User-Agent最常用
或者JavaScript判断屏幕宽度、分辨率,然后重定向到相应的mobile版

迷鸟归林 2022-08-31 10:48:03

一般是根据 useragent来判断的。

function is_mobile() {
$user_agent = $_SERVER['HTTP_USER_AGENT'];
$mobile_agents = Array("240x320","acer","acoon","acs-","abacho","ahong","airness","alcatel","amoi","android","anywhereyougo.com","applewebkit/525","applewebkit/532","asus","audio","au-mic","avantogo","becker","benq","bilbo","bird","blackberry","blazer","bleu","cdm-","compal","coolpad","danger","dbtel","dopod","elaine","eric","etouch","fly ","fly_","fly-","go.web","goodaccess","gradiente","grundig","haier","hedy","hitachi","htc","huawei","hutchison","inno","ipad","ipaq","ipod","jbrowser","kddi","kgt","kwc","lenovo","lg ","lg2","lg3","lg4","lg5","lg7","lg8","lg9","lg-","lge-","lge9","longcos","maemo","mercator","meridian","micromax","midp","mini","mitsu","mmm","mmp","mobi","mot-","moto","nec-","netfront","newgen","nexian","nf-browser","nintendo","nitro","nokia","nook","novarra","obigo","palm","panasonic","pantech","philips","phone","pg-","playstation","pocket","pt-","qc-","qtek","rover","sagem","sama","samu","sanyo","samsung","sch-","scooter","sec-","sendo","sgh-","sharp","siemens","sie-","softbank","sony","spice","sprint","spv","symbian","tablet","talkabout","tcl-","teleca","telit","tianyu","tim-","toshiba","tsm","up.browser","utec","utstar","verykool","virgin","vk-","voda","voxtel","vx","wap","wellco","wig browser","wii","windows ce","wireless","xda","xde","zte");
$is_mobile = false;
foreach ($mobile_agents as $device) {
if (stristr($user_agent, $device)) {
$is_mobile = true;
break;
}
}
return $is_mobile;
}

也可参考 https://github.com/easychen/LazyPHP/b...

我恋#小黄人 2022-08-31 10:48:03

一般从浏览器的 UA 判断浏览器和系统

还有像上面说的用的响应式设计,个人暂时倾向于判断UA

别忘他 2022-08-31 10:48:03

我也来个大礼包,直接看代码就好。

  /**
     * 手机访问重定向
     *
     * @return void
     */
    public static function mobile_location()
    {
        $_SERVER['HTTP_USER_AGENT'] = empty($_SERVER['HTTP_USER_AGENT']) ? '' : $_SERVER['HTTP_USER_AGENT'];
        $_SERVER['HTTP_ACCEPT'] = empty($_SERVER['HTTP_ACCEPT']) ? '' : $_SERVER['HTTP_ACCEPT'];
        $_SERVER['HTTP_VIA'] = empty($_SERVER['HTTP_VIA']) ? '' : $_SERVER['HTTP_VIA'];
        $browser = '';
        switch (true)
        {
            // ipad
            case (preg_match('/ipad/i', $_SERVER['HTTP_USER_AGENT'])):
                $browser = 'ipad';
                break;
            // Apple/iPhone browser renders as mobile
            case (preg_match('/Android/i', $_SERVER['HTTP_USER_AGENT'])):
                $browser = "android";
                break;
            // Apple/iPhone browser renders as mobile
            case (preg_match('/(apple|iphone|ipod)/i', $_SERVER['HTTP_USER_AGENT']) && preg_match('/mobile/i', $_SERVER['HTTP_USER_AGENT'])):
                $browser = "iphone";
                break;
            // Apple/iPhone browser renders as mobile
            case (preg_match('/IEMobile\/9\.0/i', $_SERVER['HTTP_USER_AGENT'])):
                $browser = "wp7";
                break;
            // Other mobile browsers render as mobile
            case (preg_match('/(blackberry|configuration\/cldc|hp |hp-|htc |htc_|htc-|iemobile|kindle|midp|mmp|motorola|mobile|nokia|opera mini|opera 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'])):
                $browser = "mobile";
                break;
            // Wap browser
            case (((strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'text/vnd.wap.wml') > 0) || (strpos(strtolower($_SERVER['HTTP_ACCEPT']), 'application/vnd.wap.xhtml+xml') > 0)) || ((isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])))):
                $browser = "mobile";
                break;
            // Shortend user agents
            case (in_array(strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 3)), array('lg ' => 'lg ', 'lg-' => 'lg-', 'lg_' => 'lg_', 'lge' => 'lge')));
                $browser = "mobile";
                break;
            // More shortend user agents
            case (in_array(strtolower(substr($_SERVER['HTTP_USER_AGENT'], 0, 4)), array('acs-' => 'acs-', 'amoi' => 'amoi', 'doco' => 'doco', 'eric' => 'eric', 'huaw' =>
                'huaw', 'lct_' => 'lct_', 'leno' => 'leno'
                , 'mobi' => 'mobi', 'mot-' => 'mot-', 'moto' => 'moto', 'nec-' => 'nec-
', 'phil' => 'phil', 'sams' => 'sams', 'sch-' => 'sch-', 'shar' =>
                'shar', 'sie-' => 'sie-', 'wap_' => 'wap_', 'zte-
' => 'zte-')));
                $browser = "mobile";
                break;
            // Render mobile site for mobile search engines
            case (preg_match('/Googlebot-Mobile/i', $_SERVER['HTTP_USER_AGENT']) || preg_match('/YahooSeeker\/M1A1-R2D2/i', $_SERVER['HTTP_USER_AGENT'])):
                $browser = "mobile";
                break;
            case (preg_match('/wap/i', $_SERVER['HTTP_VIA'])):
                $browser = "mobile";
                break;
        }
        return $browser;
    }
将军与妓 2022-08-31 10:48:03

useragent 判断,这是语言无关的

沙与沫 2022-08-31 10:48:03

user-agent 用来判断操作系统和浏览器类型、版本

一般手机都支持css3,可以用media query对不同屏幕宽度的手机返回不同样式的网页

酒废 2022-08-31 10:48:03

还能用响应式前端布局,非php

情仇皆在手 2022-08-31 10:48:03

UA+屏幕尺寸,分辨率等结合起来用,因为有些手机自带的浏览器不发ua的~

宫墨修音 2022-08-31 10:48:03

以下的javascript代码以及php代码可以用来判断iPhone或者iPod – 360weboy – 专注php web开发 | web前端开发。

Javascript

if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))) {
   window.location = "http://www.360weboy.com/iphone";
}

本人更倾向于使用服务器端语言,比如php来做判断:

Php

if(strstr($_SERVER['HTTP_USER_AGENT'],'iPhone') || strstr($_SERVER['HTTP_USER_AGENT'],'iPod'))
{
  header('Location: http://www.360weboy.com/iphone');
  exit();
}

360weboy – 专注php web开发 | web前端开发。感兴趣的话可以加入360weboy的qq群-197642724,或者关注360weboy的新浪或者腾讯微博,大家在一起分享讨论开发中的技术以及思想。

半﹌身腐败 2022-08-31 10:48:03

给两个通道 应该能解决问题

可以用js判断 比如
http://tangram.baidu.com/api#baidu.pl...
或者是media query
http://www.qianduan.net/responsive-we...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文