{"id":3014,"date":"2024-11-18T20:20:04","date_gmt":"2024-11-18T12:20:04","guid":{"rendered":"https:\/\/www.tra56.com\/?p=3014"},"modified":"2024-11-18T20:20:04","modified_gmt":"2024-11-18T12:20:04","slug":"3014","status":"publish","type":"post","link":"https:\/\/www.tra56.com\/3014.html","title":{"rendered":"PHP\u5224\u65ad\u662f\u5426\u652f\u4ed8\u5b9d\u3001\u5fae\u4fe1\u3001\u624b\u673a\u3001PC\u6d4f\u89c8\u5668\u65b9\u6cd5"},"content":{"rendered":"

1\u3001PHP<\/a>\u5224\u65ad\u662f\u5426\u624b\u673a\u6d4f\u89c8\u5668<\/span><\/p>\n

function isMobile() {\r\n    \/\/ \u5224\u65ad\u662f\u5426\u662f\u79fb\u52a8\u8bbe\u5907\r\n    $user_agent = $_SERVER['HTTP_USER_AGENT'];\r\n    $mobile_agents = array("Android", "iPhone", "iPad", "iPod", "Windows Phone", "BlackBerry", "Mobile");\r\n    foreach ($mobile_agents as $agent) {\r\n        if (strpos($user_agent, $agent) !== false) {\r\n            return true;\r\n        }\r\n    }\r\n    return false;\r\n}\r\nif (isMobile()) {\r\n    echo "\u5f53\u524d\u662f\u624b\u673a\u7aef";\r\n} else {\r\n    echo "\u5f53\u524d\u662fPC\u7aef";\r\n}<\/pre>\n

2\u3001PHP<\/a>\u5224\u65ad\u5fae\u4fe1\u652f\u4ed8\u5b9d\u6216\u5176\u5b83\u6d4f\u89c8\u5668<\/span><\/p>\n

function getBrowserType(){\r\n    $user_agent = $_SERVER['HTTP_USER_AGENT'];\r\n    if (strpos($user_agent, 'AlipayClient') !== false){\r\n        return 'Alipay';\r\n    }elseif (strpos($user_agent, 'MicroMessenger') !== false){\r\n        return 'WeChat';\r\n    }else{\r\n        return 'Other';\r\n    }\r\n}\r\n\/\/ \u4f7f\u7528\u65b9\u6cd5\r\n$browserType = getBrowserType();\r\nif ($browserType == 'Alipay'){\r\n    echo "\u5f53\u524d\u662f\u652f\u4ed8\u5b9d\u6d4f\u89c8\u5668";\r\n}elseif ($browserType == 'WeChat'){\r\n    echo "\u5f53\u524d\u662f\u5fae\u4fe1\u6d4f\u89c8\u5668";\r\n}else{\r\n    echo "\u5f53\u524d\u4e0d\u662f\u652f\u4ed8\u5b9d\u6d4f\u89c8\u5668\u4e5f\u4e0d\u662f\u5fae\u4fe1\u6d4f\u89c8\u5668";\r\n}<\/pre>\n
\n
  • \n

    \u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5ba2\u6237\u7aef\u53ef\u4ee5\u81ea\u884c\u8bbe\u7f6e\u548c\u66f4\u6539 HTTP_USER_AGENT<\/code> \u5b57\u6bb5\uff0c\u5e76\u4e14\u67d0\u4e9b\u5ba2\u6237\u7aef\u4e5f\u53ef\u80fd\u4f7f\u7528\u81ea\u5b9a\u4e49\u7684\u7528\u6237\u4ee3\u7406\u5b57\u7b26\u4e32\u3002\u56e0\u6b64\uff0c\u5728\u4f7f\u7528 HTTP_USER_AGENT<\/code> \u65f6\u9700\u8981\u6ce8\u610f\u5176\u53ef\u9760\u6027\u548c\u51c6\u786e\u6027\uff0c\u5e76\u7ed3\u5408\u5176\u4ed6\u7684\u68c0\u6d4b\u624b\u6bb5\u505a\u66f4\u5168\u9762\u7684\u5224\u65ad\u3002<\/p>\n<\/li>\n<\/ul>\n

    <\/p>\n","protected":false},"excerpt":{"rendered":"

    1\u3001PHP\u5224\u65ad\u662f\u5426\u624b\u673a\u6d4f\u89c8\u5668 function isMobile() {  &n […]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,92],"tags":[],"class_list":["post-3014","post","type-post","status-publish","format-standard","hentry","category-php","category-phpcode"],"_links":{"self":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/3014"}],"collection":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/comments?post=3014"}],"version-history":[{"count":1,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/3014\/revisions"}],"predecessor-version":[{"id":3015,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/posts\/3014\/revisions\/3015"}],"wp:attachment":[{"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/media?parent=3014"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/categories?post=3014"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tra56.com\/wp-json\/wp\/v2\/tags?post=3014"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}