Detect (mobile) browser and device


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

Based upon http://csswizardry.com/2010/01/iphone-css-tips-for-building-iphone-websites/


Copy this code and paste it in your HTML
  1. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  2. $iphone = strpos($user_agent,"iPhone");
  3. $ipod = strpos($user_agent,"iPod");
  4. $ipad = strpos($user_agent,"iPad");
  5. $blackberry = stristr($user_agent,"BlackBerry");
  6. $android = strpos($user_agent,"Android");
  7.  
  8. if ($iphone || $ipod || $blackberry || $android)
  9. {
  10. $browser = 'mobile phone';
  11. if($iphone) {$browser .= ' iphone';}
  12. if($ipod) {$browser .= ' ipod';}
  13. if($blackberry) {$browser .= ' blackberry';}
  14. if($android) {$browser .= ' android';}
  15. }
  16. else if($ipad)
  17. {
  18. $browser = 'mobile tablet';
  19. $browser .= ' ipad';
  20. }
  21. else
  22. {
  23. $browser = 'desktop';
  24. }
  25.  
  26.  
  27. /*
  28. To use in CSS:
  29. <body class="<?php echo $browser ?>">
  30. */

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.