简体   繁体   English

如何让php判断它是否在iOS或MacOS上运行

[英]How to get php to tell if it run is on iOS or MacOS

I have been attempting to run two different variations of my website: one for desktop and one for mobile. 我一直在尝试运行网站的两种不同版本:一种用于桌面,另一种用于移动。 I decided to make it work like this (using PHP): if on Windows, Mac, or other desktop OS, run this HTML. 我决定使它像这样(使用PHP)工作:如果在Windows,Mac或其他桌面操作系统上,请运行此HTML。 if on Android, iOS, etc. run that HTML. 如果在Android,iOS等上运行该HTML。 When I go to my website through Windows or Android, the HTML for the corresponding OS is run. 当我通过Windows或Android访问我的网站时,将运行相应操作系统的HTML。 But when I go to my website through an iPhone or MacBook, both the code for iPhone AND Mac is run, regardless of if the user is on an iPhone or MacBook: Here is the code I use: 但是,当我通过iPhone或MacBook进入网站时,无论用户是在iPhone还是MacBook上,iPhone和Mac的代码都会运行:这是我使用的代码:

<!--HTML for macOS --> 

 <?php
if(preg_match( '/macintosh|mac os x/i',  $_SERVER['HTTP_USER_AGENT'])) {

echo' <!--some HTML--> ';  }  ?>


<!--HTML for iPhone--> 


    <?php

       ifif(preg_match('/iphone/i', $_SERVER['HTTP_USER_AGENT'])) {

        echo' <!--some HTML-->  ';  }  ?>

If you want to observe the problem, go to bamfacts.tk Thank you! 如果您想观察问题,请转至bamfacts.tk,谢谢!

Following Script will tell you if it is OS X or iOS. 以下脚本会告诉您是OS X还是iOS。 You just need to see what is the HTTP_USER_AGENT and you are good to go after using some regex or string operations. 您只需要查看HTTP_USER_AGENT是什么,使用一些正则表达式或字符串操作就可以了。

<?php
// Apple detection array
$Apple = array();
$Apple['UA'] = $_SERVER['HTTP_USER_AGENT'];
$Apple['Device'] = false;
$Apple['Types'] = array('iOS', 'Macintosh');
foreach ($Apple['Types'] as $d => $t) {
    $Apple[$t] = (strpos($Apple['UA'], $t) !== false);
    $Apple['Device'] |= $Apple[$t];
}
// is this an Apple device?
echo
    "<p>Apple device? ", ($Apple['Device'] ? 'true' : 'false'),
    "</p><p>iOS? ", ($Apple['iOS'] ? 'true' : 'false'),
    "</p><p>OS X? ", ($Apple['Macintosh'] ? 'true' : 'false'),
    '</p>';
?>

You can also do it with Java Script like following: 您也可以使用Java Script进行操作,如下所示:

var Apple = {};
Apple.UA = navigator.userAgent;
Apple.Device = false;
Apple.Types = ["iOS", "Macintosh"];
for (var d = 0; d < Apple.Types.length; d++) {
    var t = Apple.Types[d];
    Apple[t] = !!Apple.UA.match(new RegExp(t, "i"));
    Apple.Device = Apple.Device || Apple[t];
}

// is this an Apple device?
alert(
    "Is it an Apple device? " + Apple.Device +
    " \nIs it iOS? " + Apple.iOS +
    " \nIs it OSX? " + Apple.Macintosh
);

You can see the Javascript fiddle here . 您可以在此处看到Javascript小提琴。

I don't think this is a goed idea, why not use a responsive design. 我不认为这是过时的想法,为什么不使用响应式设计。 Like Foundation ( http://foundation.zurb.com/ ) of Bootstrap ( http://getbootstrap.com/ ). 类似于Bootstrap( http://getbootstrap.com/ )的Foundation( http://foundation.zurb.com/ )。 Or make something yourself ( http://www.w3schools.com/html/html_responsive.asp ) 或自己动手做( http://www.w3schools.com/html/html_sensitive.asp

Responsive basically means the site scales to the format of the screen the visitor is using (phone, tablet, latop, large desktop). 响应式基本上是指网站可以缩放为访问者正在使用的屏幕格式(电话,平板电脑,笔记本电脑,大型台式机)。 This way your site is working on every devise instead of making a site for every devise. 这样,您的网站就可以在每个设计上工作,而不必为每个设计创建网站。

You can also use javascript. 您也可以使用javascript。

 /* Check if mobile */

var isMobile = {
   Android: function() {
       return navigator.userAgent.match(/Android/i);
   },
   BlackBerry: function() {
       return navigator.userAgent.match(/BlackBerry/i);
   },
   iOS: function() {
       return navigator.userAgent.match(/iPhone|iPad|iPod/i);
   },
   Opera: function() {
       return navigator.userAgent.match(/Opera Mini/i);
   },
   Windows: function() {
       return navigator.userAgent.match(/IEMobile/i) || navigator.userAgent.match(/WPDesktop/i);
   },
   any: function() {
       return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
   }
};   

if(isMobile.any())
{
 //this is mobile
}   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM