00 Votes

PHP: Check whether website is called from iPhone, iPad or Android smartphone

Question by Guest | 2012-09-17 at 02:51

I would like to check with PHP, if my site is called from an iPhone, iPad or Android phone. Is there already any PHP function for that or how do I do this?

ReplyPositiveNegative
5Best Answer5 Votes

As far as I know, there is still no separate PHP function doing this. Maybe it will be implemented, when smartphones are even more popular. Until then, I would say, you have to do it for yourself:

$android = 
  (bool) strpos($_SERVER['HTTP_USER_AGENT'], 'Android');
$ipad = 
  (bool) strpos($_SERVER['HTTP_USER_AGENT'], 'iPad');
$iphone = 
  (bool) strpos($_SERVER['HTTP_USER_AGENT'], 'iPhone');

We simply look, whether the user agent contains one of the words "Android", "iPad" or "iPhone". In the boolean variables of the same name, we write the result, true or false.

Now, it is easy to determine the device with which the visitors is calling the website:

if ($android) {
   echo 'You surf with an Android phone.';
}
 
if (!$iphone) {
   echo 'You do not surf with an iPhone.';
}
 
if ($android || $ipad || $iphone) {
   echo 'You are using a smartphone.';
}

However, you have to note, that you can easily fake your user agent, you cannot rely on that information. For example, you can use Firefox add-ons to fake being a smartphone, although you are accessing a website from your laptop or desktop PC. But since you probably only want to display a different content and it does not depend on the world and most users will even not distort their User Agent, the function should be totally fine for your purpose.
2012-09-17 at 16:59

ReplyPositive Negative
Reply

Related Topics

Android Splash Screen Tutorial

Tutorial | 0 Comments

Android Getting Sound Levels

Open Question | 1 Answer

Important Note

Please note: The contributions published on askingbox.com are contributions of users and should not substitute professional advice. They are not verified by independents and do not necessarily reflect the opinion of askingbox.com. Learn more.

Participate

Ask your own question or write your own article on askingbox.com. That’s how it’s done.