DetectFree

DetectFree is a free service which uses internally a stripped version of the Movila Detection software. DetectFree is available to anyone and offers a simple detection for mobile and non-mobile browsers. The DetectFree comes with easy-to-use interfaces. The full-featured Movila Detection is a downloadable software.

How to use

We offer a PHP API and a Javascript script for easy adoption of the service. However, you can use the DetectFree service with any programming or scripting language available, just send the HTTP headers (as below, HTTP GET request without host and connection related headers) to http://detectfree.moviladetection.com/ and read the response.

Using the HTTP POST method is also supported. In the case of the HTTP POST request, the headers have to be encoded using the application/x-www-form-urlencoded content type.

PHP API
<?php
/**
 * This file includes PHP interface for DetectFree service
 * which is a stripped service version of MovilaDetection server-side software.
 * Currently one property is available, "is_mobile".
 * @author Movila Ltd. <info@moviladetection.com>
 * @version 0.1
 */
 
/**
 * A function for getting parameters of the device
 * @property is_mobile possible values "undef", "true", "false"
 * @return new object $properties contains properties of the device
 */
function getMobileProperties() {
        $properties = new stdClass();
        
        $headers = "";
        if (function_exists("getAllHeaders")) {
                foreach (getAllHeaders() as $key => $value) {
                        $key_lower = strtolower($key);
                        if ($key_lower == "connection") continue;
                        if ($key_lower == "content-length") continue;
                        if ($key_lower == "content-type") continue;
                        if ($key_lower == "host") continue;
                        if ($key_lower == "keep-alive") continue;
                        $headers .= "$key: $value\r\n";
                }
        } else {
                foreach ($_SERVER as $key => $value) {
                        if (substr($key, 0, 5) == "HTTP_") {
                                $key = str_replace("_", " ", substr($key, 5));
                                $key = str_replace(" ", "-", ucwords(strtolower($key)));
                                $key_lower = strtolower($key);
                                if ($key_lower == "connection") continue;
                                if ($key_lower == "content-length") continue;
                                if ($key_lower == "content-type") continue;
                                if ($key_lower == "host") continue;
                                if ($key_lower == "keep-alive") continue;
                                $headers .= "$key: $value\r\n";
                        }
                }
        }
        
        $url = "http://detectfree.moviladetection.com/";
        $options = array("http" => array("method" => "GET", "header" => $headers));
        $context = stream_context_create($options);
        $contents = @file_get_contents($url, FALSE, $context);
        
        $is_mobile = "undef";
        
        if ($contents) {
                $lines = preg_split("/\r?\n/", $contents);
                
                foreach ($lines as $line) {
                        $pos = strpos($line, ":");
                        if ($pos && strlen($line) > $pos + 1) {
                                $key = substr($line, 0, $pos);
                                $value = substr($line, $pos + 1);
                                if ($key == "is_mobile")
                                        $is_mobile = $value;
                        }
                }
        }
        $properties->is_mobile = $is_mobile;
        
        return $properties;
}

/**
 * How to use the function getMobileProperties
 */
$properties = getMobileProperties();
$is_mobile = $properties->is_mobile;

// test
echo "is_mobile:$is_mobile";

?>
Javascript
<head>
        <script
                        src="http://detectfree.moviladetection.com/detectfree.js" 
                        language="javascript"
                        type="text/javascript">
        </script>
</head>

<script language="javascript" type="text/javascript">
<!--//--><![CDATA[//><!--

// test
document.write("is_mobile:" + (is_mobile ? "true" : "false"));

//--><!]]>
</script>
 
Simple Javascript domain redirection example using DetectFree, just change the domains "www.example.com" and "m.example.com"
<head>
        <script
                        src="http://detectfree.moviladetection.com/detectfree.js" 
                        language="javascript"
                        type="text/javascript">
        </script>
</head>

<script language="javascript" type="text/javascript">
<!--//--><![CDATA[//><!--

// domains have to differ i.e. the parts before the first slash "/" have to
// differ for this to work correctly
var www_domain = "www.example.com";
var mobile_domain = "m.example.com";

var cur_domain = document.domain.toLowerCase();

// notice slash "/" in the end, and remove if needed
if (is_mobile && cur_domain != mobile_domain)
        window.location = "http://" + mobile_domain + "/";
else if (!is_mobile && cur_domain != www_domain)
        window.location = "http://" + www_domain + "/";

//--><!]]>
</script>
 
Simple Javascript landing page redirection example using DetectFree, just change the URLs
<head>
        <script
                        src="http://detectfree.moviladetection.com/detectfree.js" 
                        language="javascript"
                        type="text/javascript">
        </script>
</head>

<script language="javascript" type="text/javascript">
<!--//--><![CDATA[//><!--

// URLs have to begin with the protocol, e.g. "http://"
// this example is case-insensitive
var www_landing_page = "http://www.example.com/";
var mobile_landing_page = "http://www.example.com/mobile";

var cur_url = document.URL.toLowerCase();

if (is_mobile && cur_url == www_landing_page.toLowerCase())
        window.location = mobile_landing_page;
else if (!is_mobile && cur_url == mobile_landing_page.toLowerCase())
        window.location = www_landing_page;

//--><!]]>
</script>