Mobile Redirector
Introduction
Sample
Parameters
Details
Examples
Introduction
The Mobile Redirector detects if the incoming browser is a mobile or desktop browser. You are then able to redirect mobile browsers to a mobile version of your site and leaving desktop browsers to view your current web site.
We recommend that you put this detection on your front page to provide an optimal experience for mobile users, you can include it as option on every page, but you do need to think about where on your mobile site you are going to redirect mobile users.
Sample
You call the Mobile Redirector by suppling your key and the incoming headers of the current request, for example:
http://www.mobileelements.com/MobileRedirector?key=00000000000000000000000000000000&ua=iphone
This example will match on the http request useragent and return "true" given that the device matched is a mobile device (Apple iPhone):
true
The API is intended to be called from server side code. Examples in different languages can be found below.
Parameters
Parameters are separated with the ampersand (&) character. You can specify the parameters in any order.
| Parameter | Description | Required? |
|---|---|---|
| key | Your user key (view My Account to retrieve your key) | Yes |
| ua | The useragent string to override (This is normally only passed to us for testing purposes) | No (Default is the http request useragent) |
Details
Examples
PHP
<?php
$isMobile = "";
$f = fopen( "http://www.mobileelements.com/MobileRedirector?key=00000000000000000000000000000000&ua=iphone", "r" );
while( $data = fread( $f, 4096 ) ) { $isMobile .= $data; }
fclose( $f );
if ($isMobile == "true")
{
// server side include mobile site
include("mobile.php");
}
else
{
// server side include desktop site
include("desktop.php");
}
?>
C#
using System;
using System.Net;
using System.Web;
public class MobileElements : System.Web.UI.Page
{
protected override void OnLoad(EventArgs e)
{
WebClient client = new WebClient();
client.Headers.Add(Request.Headers);
string isMobile = client.DownloadString("http://www.mobileelements.com/MobileRedirector?key=00000000000000000000000000000000");
if (isMobile == "true")
{
// server side rewrite of the current request
HttpContext.Current.RewritePath("~/mobile");
}
base.OnLoad(e);
}
}
Java
import java.io.*;
import java.net.*;
public class MobileElements {
public static void main(String[] args)
throws IOException {
URL mobileelements = new URL("http://www.mobileelements.com/MobileRedirector?key=00000000000000000000000000000000&ua=iphone");
URLConnection connection = mobileelements.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String isMobile = in.readLine();
if (isMobile == "true")
{
// server side include the mobile site
// <jsp:include page="mobile.html" />
}
else
{
// server side include the mobile site
// <jsp:include page="desktop.html" />
}
in.close();
}
}
Back to Top




+