Detecting the Kindle Fire with PHP

Mar 8 2012
Detecting the Kindle Fire with PHP

There are many reasons to detect the type of platform your user is viewing your website on. There are articles all over the internet about detecting iphones, ipads, android devices, etc. But when it comes to detecting the Kindle Fire, things can get a bit strange. To get the user’s platform, you will want to use the PHP method, $_SERVER['HTTP_USER_AGENT'] (see below).

<?php
$user_agent = $_SERVER['HTTP_USER_AGENT'];
echo $user_agent;
?>

This will return:
Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_3; en-us; Silk/1.1.0-80)
AppleWebKit/533.16 (KHTML, like Gecko) Version/5.0 Safari/533.16 Silk-Accelerated=true

As you can see, there are mentions of Mozilla, Apple, WebKit, Safari, etc. To detect the Kindle Fire, we will want to find any mention of the word ‘silk’ within the $_SERVER['HTTP_USER_AGENT'].

<?php
$user_agent = trim(strtolower($_SERVER['HTTP_USER_AGENT']));
if( strrpos( $user_agent,'silk/' ) != false && strrpos(  $user_agent,'silk-accelerated=' ) != false ) {
echo 'This is a Kindle Fire!';
}
?>

And there you have it! Place this snippet of code in your next project to detect the Kindle Fire using PHP.

One Comment to “Detecting the Kindle Fire with PHP”

  1. Very nice Colin!

    By Ben Harwell on March 8th, 2012 at 10:37 am

Leave a Reply