With the popularization of Internet mobile devices, many websites are compatible with mobile browsing. To better display webpages on mobile phones, we have chosen to use CSS media queries to create responsive templates. This article introduces PHP code to determine whether a device is a mobile phone or tablet (two methods). If you are interested, learn it together. Now mobile internet is becoming more and more developed, many websites use mobile phone browsing. To better display webpages on mobile phones, we have chosen to use CSS media queries to create responsive templates. However, this also has drawbacks, for example, the structure of some websites is CMS type, and too much content needs to be displayed. Designing a response style using CSS media queries only hides but loads the content, to make the mobile phone end display content more quickly, we can use this PHP to determine the phone device code. This code can be used to conveniently display or not display custom content.
Page matching for mobile devices is often required during WEB development. Of course, you can directly make the website responsive, but if you don't want to do this, you can use PHP to determine the device type and then display the corresponding interface and content. Today, I am sharing a PHP method to determine whether a device is a mobile phone or tablet. The method is derived from WordPress (wp-DES/vars. php: 125) and is applicable to most types of mobile phone/tablet disconnections:
Method 1:
/** * Test if the current browser runs on a mobile device (smart phone, tablet, etc.) * * @staticvar bool $is_mobile * * @return bool */function wp_is_mobile() { static $is_mobile = null; if ( isset( $is_mobile ) ) { return $is_mobile; } if ( empty($_SERVER['HTTP_USER_AGENT']) ) { $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 ) { $is_mobile = true; } else { $is_mobile = false; } return $is_mobile;}
Code 2:
This is the PHP function code used to determine the mobile phone device. It is copied to the PHP function library and called:
<? Phpfunction is_mobile () {$ user_agent = $ _ SERVER ['HTTP _ USER_AGENT ']; $ mobile_browser = Array ("mqqbrowser", // mobile QQ Browser "opera mobi ", // cell phone opera "juc", "iuc", // uc Browser "fennec", "ios", "applewebKit/420", "applewebkit/525 ", "applewebkit/532", "ipad", "iphone", "ipaq", "ipod", "iemobile", "windows ce ", // windows phone "240 × 320", "480 × 640", "acer", "android", "anywhereyougo.com", "asus", "audio", "blackberry ", "blazer", "coolpad "," Dopod "," etouch "," hitachi "," htc "," huawei "," jbrowser "," lenovo "," lg "," lg -", "lge-", "lge", "mobi", "moto", "nokia", "phone", "samsung", "sony", "symbian ", "tablet", "tianyu", "wap", "xda", "xde", "zte"); $ is_mobile = false; foreach ($ mobile_browser as $ device) {if (stristr ($ user_agent, $ device) {$ is_mobile = true; break ;}return $ is_mobile ;}?>
This is the call code. You can add if to judge:
<?php if(is_mobile()):?>
Set mobile content
<?php endif; ?>
The above is all the content of this article. I hope you will like it.