php - How to determine whether the access comes from PC or mobile?
过去多啦不再A梦
过去多啦不再A梦 2017-05-27 17:42:54
0
5
650

Mainly the judgment comes from the PC side, because some operations must be performed on the computer.
It comes from the mobile side, hiding some functions.

过去多啦不再A梦
过去多啦不再A梦

reply all (5)
迷茫

The assistant class is as follows:
The principle is to verify the header

 0) return true; else return false; } }
    Peter_Zhu

    Mobile_Detect是轻量级php库通过User-agent和特定的人HttpJudge by the head:

    • Mobile_Detect

      曾经蜡笔没有小新
      function is_mobile() { if(empty($_SERVER['HTTP_USER_AGENT']))return false; $user_agent = strtolower($_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; }
        伊谢尔伦

        Post the code in THINKcmf

        /** * 判断是否为手机访问 * @return boolean */ function sp_is_mobile() { static $sp_is_mobile; if ( isset($sp_is_mobile) ) return $sp_is_mobile; if ( empty($_SERVER['HTTP_USER_AGENT']) ) { $sp_is_mobile = false; } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.) || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mobi') !== false ) { $sp_is_mobile = true; } else { $sp_is_mobile = false; } return $sp_is_mobile; } /** * 判断是否为微信访问 * @return boolean */ function sp_is_weixin(){ if (strpos($_SERVER['HTTP_USER_AGENT'], 'MicroMessenger') !== false) { return true; } return false; }

        Of course, if you still need to judge Android or iOS, there is a simple method. Customize the value of HTTP_USER_AGENT on the mobile side, and then get $_SERVER['HTTP_USER_AGENT'] from the server. For example, add a my_webapp to determine whether this character is Existence

        $user_agent = $_SERVER['HTTP_USER_AGENT']; if (stripos($user_agent, 'my_webapp') !== false) { $this->assign("is_webapp", true); }
          phpcn_u1582

          I answered this question in segment fault, and the questioner said it was valid at that time: /q/101...

            Latest Downloads
            More>
            Web Effects
            Website Source Code
            Website Materials
            Front End Template
            About us Disclaimer Sitemap
            php.cn:Public welfare online PHP training,Help PHP learners grow quickly!