PHP method for determining the source of mobile devices _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP determines the source of a mobile device. PHP's method of judging the source of mobile devices is now in the hot age of mobile devices. maybe we often need to use page matching for mobile devices during web front-end development. Of course, we can use PHP to determine the source of mobile devices.

In the era of mobile device popularity, we may often need to use page matching for mobile devices when developing web front-end. Of course, we can use responsive page design to deal with front-end matching issues. However, a responsive page only does not display some code, but will still be loaded to users' mobile devices. In order to better take care of the user's mobile browsing experience and reduce unnecessary loading. We can use PHP to reduce code loading and improve the user experience when browsing the page. Because there are a wide variety of mobile devices and a wide variety of browsers, the problem cannot be completely solved simply by using the User-Agent.

Function isMobile ()

{

// If HTTP_X_WAP_PROFILE exists, it must be a mobile device.

If (isset ($ _ SERVER ['http _ X_WAP_PROFILE '])

{

Return true;

}

// If the via information contains wap, it must be a mobile device. some service providers will block this information.

If (isset ($ _ SERVER ['http _ vean'])

{

// Flase cannot be found; otherwise, true

Return stristr ($ _ SERVER ['http _ vean'], "wap ")? True: false;

}

// Determine the client flag sent by the mobile phone. the compatibility needs to be improved.

If (isset ($ _ SERVER ['http _ USER_AGENT '])

{

$ Clientkeywords = array ('Nokia ',

'Sony ',

'Ericsson ',

'MOT ',

'Samsung ',

'HTC ',

'Sgh ',

'LG ',

'Sharp ',

'Sie -',

'Philips ',

'Panasonic ',

'Alcatel ',

'Lenovo ',

'IPhone ',

'IPod ',

'BlackBerry ',

'Meizu ',

'Android ',

'Netfront ',

'Symbian ',

'Ucweb ',

'Windowsce ',

'Palm ',

'Operamini ',

'Operamobi ',

'Openwave ',

'Nexusone ',

'Cldc ',

'Midp ',

'Wap ',

'Mobile'

);

// Search for the keyword of the mobile browser from HTTP_USER_AGENT

If (preg_match ("/(". implode ('|', $ clientkeywords). ")/I", strtolower ($ _ SERVER ['http _ USER_AGENT '])

{

Return true;

}

}

// Resolution, which may be inaccurate and placed in the final judgment

If (isset ($ _ SERVER ['http _ ACCEPT '])

{

// If only wml is supported and html is not supported, it must be a mobile device.

// If wml and html are supported, but wml is a mobile device before html

If (strpos ($ _ SERVER ['http _ ACCEPT '], 'vnd. wap. wml ')! = False) & (strpos ($ _ SERVER ['http _ ACCEPT '], 'text/html ') === false | (strpos ($ _ SERVER ['http _ ACCEPT '], 'vnd. wap. wml ') <strpos ($ _ SERVER ['http _ ACCEPT'], 'text/html '))))

{

Return true;

}

}

Return false;

}

?>

Currently, this method can be used for most mobile devices. you can also add some UA judgment parameters as needed.

In the era when mobile devices are booming, we may often need to use page matching for mobile devices when developing web front-ends. Of course we can...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.