0

I need a way in php to be able to know when my website is being opened on mobile/tablet. I need specific design for each device (iPhone, android and tablet’s)

I’ve tried this but its not working properly:

<?php   if(stristr($_SERVER['HTTP_USER_AGENT'], "Mobile"))
         { // if mobile browser ?>      
             <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/mobile.css" type="text/css" media="screen" />  
         <?php  }

Can anyone help me?

Vishva Dave
  • 5,037
  • 2
  • 27
  • 51

2 Answers2

1

You can use a php library for this purpose:

https://github.com/serbanghita/Mobile-Detect

It has a lot of functions to detect any device eg: isMobile(), isTablet(), isiPhone() etc.

Only for php:

<?php
// include library file
require_once 'Mobile_Detect.php';
$object = new Mobile_Detect;


//Now detect device
<?php
// Return true or false based on your device
$detect->isMobile();
$detect->isTablet();


<?php
// Suppose you want to check for mobile environment.
if ($detect->isMobile()) {
    // Your code here.
}
Arshad Hussain
  • 743
  • 5
  • 22
0
<link rel="stylesheet" href="style.css" />

<link rel="stylesheet" media="screen and (min-device-width: 800px)" href="mobile.css" />

use both the stylesheets as above. So if the device width is 800px or lesser the mobile.css would be used to render. Or else by default the style.css is used.