Home  >  Article  >  Backend Development  >  PHP determines whether to access the website from a mobile phone or a computer

PHP determines whether to access the website from a mobile phone or a computer

藏色散人
藏色散人Original
2020-08-01 09:33:202779browse

php method to determine whether a mobile phone or a computer is accessing a website: first create a PHP sample file; then define an "is_mobile" method; then implement the function of determining which browser is being accessed; and finally, in The browser can execute the file.

PHP determines whether to access the website from a mobile phone or a computer

Recommended: "PHP Video Tutorial"

php determines whether to access computer or mobile phone:

The number of mobile Internet users is increasing. Nowadays, various websites have launched mobile websites. When computer users visit, they directly access the computer version of the web page. When users access the website through their mobile phones, they automatically jump to the mobile version. Web page, let me share with you a piece of code in php to determine whether computer access or mobile access:

<?php
//手机网页跳转
//如果检测到访问的浏览器为下列一个指定的移动浏览器 则返回true
function is_mobile(){
    $regex_match="/(nokia|iphone|android|motorola|^mot\-|softbank|foma|docomo|kddi|up\.browser|up\.link|";
    $regex_match.="htc|dopod|blazer|netfront|helio|hosin|huawei|novarra|CoolPad|webos|techfaith|palmsource|";
    $regex_match.="blackberry|alcatel|amoi|ktouch|nexian|samsung|^sam\-|s[cg]h|^lge|ericsson|philips|sagem|wellcom|bunjalloo|maui|";  
    $regex_match.="symbian|smartphone|midp|wap|phone|windows ce|iemobile|^spice|^bird|^zte\-|longcos|pantech|gionee|^sie\-|portalmmm|";
    $regex_match.="jig\s browser|hiptop|^ucweb|^benq|haier|^lct|opera\s*mobi|opera\*mini|320x320|240x320|176x220";
    $regex_match.=")/i";      
    return isset($_SERVER[&#39;HTTP_X_WAP_PROFILE&#39;]) or isset($_SERVER[&#39;HTTP_PROFILE&#39;]) or preg_match($regex_match, strtolower($_SERVER[&#39;HTTP_USER_AGENT&#39;]));
}
     
$is_mobile=is_mobile();
 
if($is_mobile){
    //这是一个手机浏览器,可以跳转到手机版网页
    //header("Location: http://www.abc.com/3g");
    echo "手机访问";
  }else{
    //这不是一个手机浏览器
    //header("Location: http://www.abc.com/desktop");
    echo "电脑访问";
  }
?>

The above is the detailed content of PHP determines whether to access the website from a mobile phone or a computer. 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