Server-side mobile device detection


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  1. class Client
  2. {
  3. /**
  4. * Available Mobile Clients
  5. *
  6. * @var array
  7. */
  8. private $_mobileClients = array(
  9. "midp",
  10. "240x320",
  11. "blackberry",
  12. "netfront",
  13. "nokia",
  14. "panasonic",
  15. "portalmmm",
  16. "sharp",
  17. "sie-",
  18. "sonyericsson",
  19. "symbian",
  20. "windows ce",
  21. "benq",
  22. "mda",
  23. "mot-",
  24. "opera mini",
  25. "philips",
  26. "pocket pc",
  27. "sagem",
  28. "samsung",
  29. "sda",
  30. "sgh-",
  31. "vodafone",
  32. "xda",
  33. "iphone",
  34. "android"
  35. );
  36.  
  37. /**
  38. * Check if client is a mobile client
  39. *
  40. * @param string $userAgent
  41. * @return boolean
  42. */
  43. public function isMobileClient($userAgent)
  44. {
  45. $userAgent = strtolower($userAgent);
  46. foreach($this->_mobileClients as $mobileClient) {
  47. if (strstr($userAgent, $mobileClient)) {
  48. return true;
  49. }
  50. }
  51. return false;
  52. }
  53.  
  54. }
  55.  
  56. $client = new Client();
  57. $client->isMobileClient($_SERVER['HTTP_USER_AGENT']);

URL: http://www.phpdevblog.net/2009/01/detecting-mobile-devices.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.