Hướng dẫn php detect tablet - php phát hiện máy tính bảng

Ngày 26 tháng 7 năm 2013, bởi Ruadhán O'Donoghue, by Ruadhán O'Donoghue, by Ruadhán O'Donoghue

Nội dung chính ShowShow

  • Làm thế nào bạn có thể phát hiện một thiết bị di động bằng PHP?
  • Làm cách nào để có được chi tiết thiết bị trong PHP?
  • Cách tốt nhất để phát hiện JavaScript thiết bị di động là gì?
  • Làm thế nào để bạn phát hiện xem trang web đang được mở trong thiết bị di động hay máy tính để bàn trong JavaScript?

Trước đây trên Mobiforge Ronan đã đăng về một cách tiếp cận nhẹ để phát hiện thiết bị bằng cách sử dụng các biểu thức thông thường trong PHP. Vì đây vẫn là một cách tiếp cận được nhiều nhà phát triển web áp dụng ngày nay, chúng tôi cảm thấy rằng đó là khoảng thời gian chúng tôi xem lại bài viết gốc.

Đối với những người mới tham gia chủ đề, cách tiếp cận hoạt động bằng cách khớp các chuỗi thiết bị di động của người dùng bằng cách sử dụng các biểu thức thông thường. Như chúng tôi đã đề cập trong bài viết gốc, sử dụng giải pháp PHP Regex để phát hiện các thiết bị có thể là một giải pháp tốt cho các trường hợp sử dụng nhất định. Ví dụ, đối với các trang web nhỏ hơn mà không có lưu lượng truy cập lớn hoặc nơi chủ sở hữu trang web không quá quan tâm đến việc phát hiện lưu lượng di động của họ với độ chính xác rất cao hoặc mục tiêu là chuyển hướng di động hoặc máy tính bảng đơn giản.

Trong bài viết này, chúng tôi cung cấp một phiên bản cập nhật của thuật toán gốc bao gồm phát hiện máy tính bảng rõ ràng, cho những người muốn tối ưu hóa cho khách truy cập máy tính bảng của họ và chúng tôi cũng mở rộng các mẫu regex gốc để bắt một số thiết bị di động gần đây không được phát hiện bởi bản gốc.

Nhưng trước khi chúng tôi nhảy vào, có những hạn chế mà bạn nên biết trước khi chọn tùy chọn này.

  • Độ chính xác-Kết hợp tác nhân người dùng Regex có thể hoạt động tốt trong trường hợp chung, nhưng chắc chắn sẽ không nhận ra một phần của thiết bị một cách chính xác, ví dụ: Một số máy tính bảng Android. Và mặc dù có thể thêm các mẫu cụ thể hơn vào regex để phù hợp với các trường hợp cạnh bằng cách khớp với các số mô hình cụ thể, điều này có nhược điểm là giảm sự hoàn hảo của phát hiện và sẽ có ý nghĩa hiệu suất gõ cho trang web của bạn – Regex User-Agent matching can work well in the general case, but will inevitably fail to recognise some portion of devices correctly, e.g. certain Android tablets. And while it is might be possible to add more specific patterns to the regex to match edge-cases by matching specific model numbers, this has the disadvantage of reducing the perfomance of the detection, and will have knock-on performance implications for your site – Regex User-Agent matching can work well in the general case, but will inevitably fail to recognise some portion of devices correctly, e.g. certain Android tablets. And while it is might be possible to add more specific patterns to the regex to match edge-cases by matching specific model numbers, this has the disadvantage of reducing the perfomance of the detection, and will have knock-on performance implications for your site
  • Hiệu suất - Biểu thức chính quy có thể chậm thực hiện, đặc biệt đối với các mẫu phức tạp. Nếu hiệu suất là yếu tố quan trọng đối với bạn, thì đây không phải là cách để đi – Regular expressions can be slow to execute, particularly for complicated patterns. If performance is a key factor for you, then this is not the way to go – Regular expressions can be slow to execute, particularly for complicated patterns. If performance is a key factor for you, then this is not the way to go
  • Bảo trì - Các mẫu Regex sẽ cần được cập nhật thường xuyên để cập nhật các thiết bị mới có thể chưa được đề cập – The regex patterns will need to be updated regularly to keep up to date with new devices that may not be already covered – The regex patterns will need to be updated regularly to keep up to date with new devices that may not be already covered
  • Khả năng của thiết bị - Nếu bạn cần bất cứ điều gì nhiều hơn định tuyến lưu lượng truy cập đơn giản (di động/máy tính bảng/máy tính để bàn), ví dụ, nếu bạn cần biết các thuộc tính thiết bị như kích thước màn hình, giới hạn bộ nhớ, hỗ trợ HTML5, v.v. cho bạn – If you need anything more than simple traffic routing (mobile/tablet/desktop), for instance if you need to know device properties such as screen size, memory limit, HTML5 support, and so on, then this is not the solution for you – If you need anything more than simple traffic routing (mobile/tablet/desktop), for instance if you need to know device properties such as screen size, memory limit, HTML5 support, and so on, then this is not the solution for you

Vì vậy, với những cảnh báo đó, thời gian cho mã:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

$tablet_browser=0;=0;=0;

$mobile_browser=0;=0;=0;

if (preg_match ('/(máy tính bảng | ipad | playbook) | (android (?!.*(mobi | opera mini)))/i', strtolower ($ _ server ['http_user_agent']))(preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i',strtolower($_SERVER['HTTP_USER_AGENT']))){(preg_match('/(tablet|ipad|playbook)|(android(?!.*(mobi|opera mini)))/i',strtolower($_SERVER['HTTP_USER_AGENT']))){

    $tablet_browser++;$tablet_browser++;$tablet_browser++;

}

if (preg_match ('/(up.browser | up.link | mmp | symbian | điện thoại thông minh | midp | wap | phone |(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i',strtolower($_SERVER['HTTP_USER_AGENT']))){(preg_match('/(up.browser|up.link|mmp|symbian|smartphone|midp|wap|phone|android|iemobile)/i',strtolower($_SERVER['HTTP_USER_AGENT']))){

    $mobile_browser++;$mobile_browser++;$mobile_browser++;

}

if((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0)or((isset($_SERVER['HTTP_X_WAP_PROFILE'])orisset($_SERVER['HTTP_PROFILE'])))){((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0)or((isset($_SERVER['HTTP_X_WAP_PROFILE'])orisset($_SERVER['HTTP_PROFILE'])))){((strpos(strtolower($_SERVER['HTTP_ACCEPT']),'application/vnd.wap.xhtml+xml')>0)or ((isset($_SERVER['HTTP_X_WAP_PROFILE'])orisset($_SERVER['HTTP_PROFILE'])))){

    $mobile_browser++;$mobile_browser++;$mobile_browser++;

}

$mobile_ua=strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));=strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));=strtolower(substr($_SERVER['HTTP_USER_AGENT'],0,4));

$mobile_agents=array(=array(=array(

if (preg_match ('/(up.browser | up.link | mmp | symbian | điện thoại thông minh | midp | wap | phone |'w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac','w3c ','acs-','alav','alca','amoi','audi','avan','benq','bird','blac',

    'blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno','blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno','blaz','brew','cell','cldc','cmd-','dang','doco','eric','hipt','inno',

    'ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-','ipaq','java','jigs','kddi','keji','leno','lg-c','lg-d','lg-g','lge-',

    'maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-','maui','maxo','midp','mits','mmef','mobi','mot-','moto','mwbp','nec-',

    'newt','noki','palm','pana','pant','phil','play','port','prox','newt','noki','palm','pana','pant','phil','play','port','prox','newt','noki','palm','pana','pant','phil','play','port','prox',

    'qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar','qwap','sage','sams','sany','sch-','sec-','send','seri','sgh-','shar',

    'sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-','sie-','siem','smal','smar','sony','sph-','symb','t-mo','teli','tim-',

    'tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp','tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp','tosh','tsm-','upg1','upsi','vk-v','voda','wap-','wapa','wapi','wapp',

& nbsp; & nbsp; & nbsp;'wapr','webc','winw','winw','xda ','xda-');'wapr','webc','winw','winw','xda ','xda-');

if(in_array($mobile_ua,$mobile_agents)){(in_array($mobile_ua,$mobile_agents)){(in_array($mobile_ua,$mobile_agents)){

    $mobile_browser++;$mobile_browser++;$mobile_browser++;

}

if (preg_match ('/(up.browser | up.link | mmp | symbian | điện thoại thông minh | midp | wap | phone |(strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini')>0){(strpos(strtolower($_SERVER['HTTP_USER_AGENT']),'opera mini')>0){

    $mobile_browser++;$mobile_browser++;$mobile_browser++;

& nbsp; & nbsp; & nbsp;//Check for tablets on opera mini alternative headers//Check for tablets on opera mini alternative headers

    $stock_ua=strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));$stock_ua=strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));$stock_ua= strtolower(isset($_SERVER['HTTP_X_OPERAMINI_PHONE_UA'])?$_SERVER['HTTP_X_OPERAMINI_PHONE_UA']:(isset($_SERVER['HTTP_DEVICE_STOCK_UA'])?$_SERVER['HTTP_DEVICE_STOCK_UA']:''));

& nbsp; & nbsp; & nbsp;if(preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i',$stock_ua)){if(preg_match('/(tablet|ipad|playbook)|(android(?!.*mobile))/i',$stock_ua)){

      $tablet_browser++;$tablet_browser++;$tablet_browser++;

    }}}

}

if($tablet_browser>0){($tablet_browser>0){($tablet_browser>0){

if (preg_match ('/(up.browser | up.link | mmp | symbian | điện thoại thông minh | midp | wap | phone |// do something for tablet devices// do something for tablet devices

& nbsp; & nbsp; máy tính bảng in ';print'is tablet';print'is tablet';

}

elseif($mobile_browser>0){if($mobile_browser>0){if($mobile_browser>0){

& nbsp; & nbsp; // Làm điều gì đó cho thiết bị di động// do something for mobile devices// do something for mobile devices

& nbsp; & nbsp; Print'is Mobile ';print'is mobile';print'is mobile';

}

else{{{

& nbsp; & nbsp; // Làm điều gì đó cho thiết bị di động// do something for everything else// do something for everything else

& nbsp; & nbsp; Print'is Mobile ';print'is desktop';print'is desktop';

& nbsp; & nbsp; // làm điều gì đó cho mọi thứ khác     

& nbsp; & nbsp; Frint'is Desktop ';

} & nbsp; & nbsp;

?>

Như trong bài viết trước của chúng tôi, mã dựa trên công việc của Andy Moore. Cách tiếp cận được thực hiện để phát hiện máy tính bảng là một mẫu phù hợp chung, (chủ yếu). Nó sẽ hoạt động hợp lý tốt, và sẽ phù hợp với bạn nếu phát hiện chính xác mỗi thiết bị máy tính bảng là không quan trọng. Nó có thể sẽ thất bại trên một phần nhất định của máy tính bảng rất khó để phân biệt với các đối tác di động của họ mà không chỉ định các số mô hình riêng lẻ.

Làm thế nào bạn có thể phát hiện một thiết bị di động bằng PHP?

Bạn có thể sử dụng http_user_agent để kiểm tra loại thiết bị (di động/máy tính bảng/máy tính để bàn) và nền tảng (windows/android/iOS) bằng PHP.use HTTP_USER_AGENT to check device type (mobile/tablet/desktop) and platform (windows/android/iOS) using PHP.use HTTP_USER_AGENT to check device type (mobile/tablet/desktop) and platform (windows/android/iOS) using PHP.

Làm cách nào để có được chi tiết thiết bị trong PHP?

php $ userAgent = $ _ server ['http_user_agent'];if (preg_match ('/(android | bb \ d+| meego) ....

Giải nén tệp vào một thư mục trong máy chủ PHP của bạn ..

Sau đó thêm mã sau vào trang PHP của bạn:.

Tất cả thông tin thiết bị có sẵn sẽ được chứa trong mảng $ _51D:.

Cách tốt nhất để phát hiện JavaScript thiết bị di động là gì?

matchmedia () cửa sổ.MatchMedia () là một trong những thuộc tính tốt nhất để phát hiện người dùng di động với JavaScript. The Window. matchMedia() is one of the best properties for detecting mobile users with JavaScript. The Window. matchMedia() is one of the best properties for detecting mobile users with JavaScript.

Làm thế nào để bạn phát hiện xem trang web đang được mở trong thiết bị di động hay máy tính để bàn trong JavaScript?

Chúng tôi có thể sử dụng các truy vấn phương tiện CSS để kiểm tra xem trang web có được mở bên trong trình duyệt web hay trình duyệt di động hay không.Thông tin này có thể được tìm nạp bằng cách sử dụng chiều rộng tối thiểu và chiều rộng tối đa của trang web.use the CSS media queries to check whether the website is opened inside a web browser or a mobile browser. This information can be fetched using the min-width and the max-width of the webpage.use the CSS media queries to check whether the website is opened inside a web browser or a mobile browser. This information can be fetched using the min-width and the max-width of the webpage.