- 论坛徽章:
- 0
|
Mobile Detect Class
Java代码- 1.<?php<br>class Mobile_Detect
- 2.{
- 3.
- 4. protected $accept;
- 5. protected $userAgent;
- 6. protected $isMobile = false;
- 7. protected $isAndroid = null;
- 8. protected $isAndroidtablet = null;
- 9. protected $isIphone = null;
- 10. protected $isIpad = null;
- 11. protected $isBlackberry = null;
- 12. protected $isBlackberrytablet = null;
- 13. protected $isOpera = null;
- 14. protected $isPalm = null;
- 15. protected $isWindows = null;
- 16. protected $isWindowsphone = null;
- 17. protected $isGeneric = null;
- 18. protected $devices = array(
- 19. "android" => "android.*mobile",
- 20. "androidtablet" => "android(?!.*mobile)",
- 21. "blackberry" => "blackberry",
- 22. "blackberrytablet" => "rim tablet os",
- 23. "iphone" => "(iphone|ipod)",
- 24. "ipad" => "(ipad)",
- 25. "palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
- 26. "windows" => "windows ce; (iemobile|ppc|smartphone)",
- 27. "windowsphone" => "windows phone os",
- 28. "generic" => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)"
- 29. );
- 30.
- 31. public function __construct()
- 32. {
- 33. $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
- 34. $this->accept = $_SERVER['HTTP_ACCEPT'];
- 35.
- 36. if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE'])) {
- 37. $this->isMobile = true;
- 38. } elseif (strpos($this->accept, 'text/vnd.wap.wml') > 0 || strpos($this->accept, 'application/vnd.wap.xhtml+xml') > 0) {
- 39. $this->isMobile = true;
- 40. } else {
- 41. foreach ($this->devices as $device => $regexp) {
- 42. if ($this->isDevice($device)) {
- 43. $this->isMobile = true;
- 44. }
- 45. }
- 46. }
- 47. }
- 48.
- 49. /**
- 50. * Overloads isAndroid() | isAndroidtablet() | isIphone() | isIpad() | isBlackberry() | isBlackberrytablet() | isPalm() | isWindowsphone() | isWindows() | isGeneric() through isDevice()
- 51. *
- 52. * @param string $name
- 53. * @param array $arguments
- 54. * @return bool
- 55. */
- 56. public function __call($name, $arguments)
- 57. {
- 58. $device = substr($name, 2);
- 59. if ($name == "is" . ucfirst($device) && array_key_exists(strtolower($device), $this->devices)) {
- 60. return $this->isDevice($device);
- 61. } else {
- 62. trigger_error("Method $name not defined", E_USER_WARNING);
- 63. }
- 64. }
- 65.
- 66. /**
- 67. * Returns true if any type of mobile device detected, including special ones
- 68. * @return bool
- 69. */
- 70. public function isMobile()
- 71. {
- 72. return $this->isMobile;
- 73. }
- 74.
- 75. protected function isDevice($device)
- 76. {
- 77. $var = "is" . ucfirst($device);
- 78. $return = $this->$var === null ? (bool) preg_match("/" . $this->devices[strtolower($device)] . "/i", $this->userAgent) : $this->$var;
- 79. if ($device != 'generic' && $return == true) {
- 80. $this->isGeneric = false;
- 81. }
- 82.
- 83. return $return;
- 84. }
- 85.
- 86.}
复制代码 Java代码- 1.Include and instantiate the class:
- 2.
- 3.include("Mobile_Detect.php");
- 4.$detect = new Mobile_Detect();
- 5.Check for a specific platform:
- 6.
- 7.if ($detect->isAndroid()) {
- 8. // code to run for the Google Android platform
- 9.}
- 10.Available methods are isAndroid(), isAndroidtablet(), isIphone(), isIpad(), isBlackberry(), isBlackberrytablet(), isPalm(), isWindowsphone(), isWindows(), isGeneric(). Alternatively, if you are only interested in checking to see if the user is using a mobile device, without caring for specific platform:
- 11.
- 12.if ($detect->isMobile()) {
- 13. // any mobile platform
- 14.}
复制代码 |
|