23May/110
Detect Mobile Devices with PHP
Detecting when your visitors are using mobile devices may be very useful, and sometimes you don't need or want a complete detection plugin with advanced functionalities. So for those cases where you only need basic Mobile OS recognition, here's a very simple PHP function you can integrate to your scripts:
/**
* Based on the HTTP_USER_AGENT, this function tries to detect a
* mobile user agent and returns its name in a string.
* @return A string that represents the mobile user agent when one
* is found, false otherwise.
* @author Matt (www.evilcodingmonkey.com)
*/
function getMobileAgent() {
$mobileUA = false;
// Make sure the user agent is set
if(isset($_SERVER['HTTP_USER_AGENT'])) {
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT']);
if(strpos($userAgent, 'android')) {
$mobileUA = 'android';
}
elseif(strpos($userAgent, 'iphone') ) {
$mobileUA = 'iphone';
}
elseif(strpos($userAgent, 'ipad')) {
$mobileUA = 'ipad';
}
elseif(strpos($userAgen, 'windows phone os 7.0')) {
$mobileUA = 'winphone7'
}
elseif(strpos($userAgent, 'blackberry')) {
$mobileUA = 'blackberry';
}
}
return $mobileUA;
}
This very simple function will tell you when a user is behind an iPhone, iPad, Android, Windows Phone 7 or Blackberry. It returns false when none of these user agents are detected. Depending on your specific needs, you way need more specific information or more advanced features. There are several open source offers out there, such as this one: Detect Mobile Browser. Also, feel free to use this code and modify it as you wish.
Tagged as: mobile, operating system, phone, PHP, user agent
No Comments
Menu
Categories
- Android (3)
- Augmented Reality (3)
- Flex (2)
- iOS (1)
- Linux (2)
- Miscellaneous (5)
- Music (1)
- Office (2)
- PHP (1)
Links
- ADnD Downloads My D&D website
- Morningless My band’s website
Tag Cloud
3d
adb
adt
android
augmented reality
chrome
chromium
correction
dictionary
download
drm-free
eclipse
emp
emusic
extension
firefox
flash
flash builder
flex
ide
ios
language
library
libreoffice
manager
mobile
mp3
music
openoffice
operating system
package
performance
phone
PHP
player
plugin
qcar
qualcomm
remove
setup
TextView
ubuntu
underline
user agent
windows
Monthly Archives
- July 2012 (1)
- May 2012 (2)
- April 2012 (1)
- March 2012 (2)
- December 2011 (1)
- November 2011 (1)
- September 2011 (1)
- August 2011 (1)
- June 2011 (3)
- May 2011 (5)
- April 2011 (1)