Reputation: 75
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. if on Android, iOS, etc. run that HTML. When I go to my website through Windows or Android, the HTML for the corresponding OS is run. 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:
<!--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!
Upvotes: 0
Views: 996
Reputation: 5628
Following Script will tell you if it is OS X or 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.
<?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:
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.
Upvotes: 4
Reputation: 143
You can also use 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
}
Upvotes: 0
Reputation: 64
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/). Or make something yourself (http://www.w3schools.com/html/html_responsive.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.
Upvotes: 0