×
Namespaces

Variants
Actions

How to get IP address in a Nokia Asha Web App

From Nokia Developer Wiki
Jump to: navigation, search

This article explains how one can find out public IP address of your device and Web App proxy server. Solution uses a simple PHP script that returns these values to your web app in a JSON format.

Article Metadata
Compatibility
Platform(s): Series 40
Device(s): All having Nokia Browser
Article
Created: isalento (30 Dec 2011)
Last edited: hamishwillee (09 May 2013)

Contents

Introduction

About IP addresses

First thing to note, is that public IP address obtained with this solution, is not the real private IP address of your device. In most cases you will see outbound IP address of proxy or router in your network.

Nokia Asha Web Apps Proxy server appends IP address of the client, connecting Web App Proxy, to HTTP_X_FORWARDED_FOR header. HTTP_X_FORWARDED_FOR header is commonly used for this purpose by other proxies as well, thus its value might be comma separated list of IP addresses.

For example, when you are using Nokia Browser to open a Web App using WLAN connection, you will probably only see router’s IP address in HTTP_X_FORWARDED_FOR header. Same practice applies to 2G/3G connections. There it is likely that you will see IP address of your operator’s router. When web app proxy is used, REMOTE_ADDR holds the IP address of the proxy. If you are running this script in a local preview mode in Web App simulator, Web App proxy is not used and REMOTE_ADDR is your computer’s / router’s IP address.


JavaScript

Getting the public IP address requires a query to be made to a server. As a response to this query IP address information is returned in a JSON format.

function checkIp(){
req = new XMLHttpRequest();
 
var url = "www.replacewithyourserveranddomain.net/ip.php";
 
req.open("GET", url , true);
 
req.onreadystatechange = function(){
if(req.readyState == 4) {
if (req.status == 200){
 
var ip = eval('(' + req.responseText + ')');
 
if(ip.HTTP_X_FORWARDED_FOR != ""){
log("Web App proxy server address: "+ ip.REMOTE_ADDR);
log("Public IP address(s): " + ip.HTTP_X_FORWARDED_FOR);
}else{
log("Public IP address(s):" + ip.REMOTE_ADDR);
}
}else{
log("Error:" + req.status+ " "+ req.statusText);
}
}
};
req.send("");
}
 
function log(text) {
document.body.innerHTML += text+"<br>";
}

PHP script

Following short PHP script acts as a backend for our IP API.

<?php 
//Gets the IP addresses
$server_ip = getenv("REMOTE_ADDR") ;
$device_ip = getenv("HTTP_X_FORWARDED_FOR");
echo '{ "REMOTE_ADDR": "' . $server_ip . '","HTTP_X_FORWARDED_FOR":"'. $device_ip . '"}';
 
?>
This page was last modified on 9 May 2013, at 13:49.
103 page views in the last 30 days.
×