Menu

Using PHP to Detect iOS & Mobile Users
📰

Using PHP to Detect iOS & Mobile Users

DEV Community: html·Kevinleary.net·12 days ago
#2HJLDcMJ
#devto2#devto#dev#device#mobile
Reading 0:00
15s threshold

This basic mobile_user_agent_switch() function will allow you to use PHP to detect iOS and mobile device visitors using one of the following:

Supported Devices

iPhone

iPad

Android

Blackberry

The mobile_user_agent_switch() Function

/* * Mobile device detection */ if ( ! function_exists ( 'mobile_user_agent_switch' ) ){ function mobile_user_agent_switch (){ $device = '' ;

if ( stristr ( $_SERVER [ 'HTTP_USER_AGENT' ], 'ipad' ) ) { $device = "ipad" ; } else if ( stristr ( $_SERVER [ 'HTTP_USER_AGENT' ], 'iphone' ) || strstr ( $_SERVER [ 'HTTP_USER_AGENT' ], 'iphone' ) ) { $device = "iphone" ; } else if ( stristr ( $_SERVER [ 'HTTP_USER_AGENT' ], 'blackberry' ) ) { $device = "blackberry" ; } else if ( stristr ( $_SERVER [ 'HTTP_USER_AGENT' ], 'android' ) ) { $device = "android" ; }

if ( $device ) { return $device ; } return false ; { return false ; } } }

Enter fullscreen mode

Exit fullscreen mode

The function will return one of the following values for you to work with in your…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.