Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

IP Address to Country in Coldfusion

Posted on 2013-01-10
5
Medium Priority
?
296 Views
Last Modified: 2013-01-11
Hello experts.
I'm trying to use this php tutorial:http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_3437-IP-Address-to-Country-in-PHP.html

I added the data into my db. and i need a little help now with the second code (step 4) .
The question is what are these two lines doing:
// IP ADDRESSES ARE UNSIGNED AND MAY RETURN NEGATIVE VALUES
$ip_number = sprintf('%u', $x);

My code is working but i did't add the function above for negative values and the validation.
Any help to complete it?
<cfscript>
/**
* Generates an (IPv4) Internet Protocol dotted address (aaa.bbb.ccc.ddd) from the proper address representation. Returns 0 if error occurs.
*
* @param longip Numeric value of the address you want to convert. (Required)
* @return Returns a String.
* @author Troy Pullis (tpullis@yahoo.com)
* @version 1, Jan 5, 2009
*/
function long2ip(longip)
{
   var ip = "";
   var i = "";
if (longip < 0 || longip > 4294967295)
      return 0;
for (i=3;i>=0;i--) {
ip = ip & int(longip / 256^i);
longip = longip - int(longip / 256^i) * 256^i;
if (i>0)
         ip = ip & ".";
}
return ip;
}

/**
* Converts a string containing an (IPv4) Internet Protocol dotted address (aaa.bbb.ccc.ddd) into a proper address representation. Returns 0 if error occurs.
*
* @param ip Dotted address value you want to convert. (Required)
* @return Returns a String.
* @author Troy Pullis (tpullis@yahoo.com)
* @version 1, Jan 5, 2009
*/
function ip2long(ip) {
   var iparr = ListToArray(ip,".");
   if (ArrayLen(iparr) != 4)
      return 0;
   else
       return iparr[1]*256^3 + iparr[2]*256^2 + iparr[3]*256 + iparr[4];
}
</cfscript>
<!--- <cfset myip = CGI.REMOTE_ADDR /> --->
<cfset myip = '85.75.31.212'/>
<cfset myip2long = ip2long(myip) />
<!--- <cfdump var="#myip2long#"> --->
<cfquery name="getcountry" datasource="#request.dsn#">
SELECT country_code, country_name FROM ip2country WHERE #myip2long# BETWEEN ip_number_lo AND ip_number_hi LIMIT 1
</cfquery>
<cfdump var="#getcountry#">

Open in new window

0
Comment
Question by:Panos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 52

Accepted Solution

by:
_agx_ earned 2000 total points
ID: 38763807
I don't know php, but the docs say "%u" returns an "unsigned integer representation of a positive integer". Sounds like it just means the absolute value of the number ie

               ie  <cfset something = abs( x )>

But I'd ask a php person to be sure ...
0
 
LVL 2

Author Comment

by:Panos
ID: 38764128
Thank you agx.
0
 
LVL 2

Author Comment

by:Panos
ID: 38766731
0
 
LVL 2

Author Closing Comment

by:Panos
ID: 38766733
Thank you very much
regards
panos
0
 
LVL 52

Expert Comment

by:_agx_
ID: 38768287
Hm.. in that thread are they saying that "%u" just removes the sign from an integer?  I think so, but the reason for asking is that some languages like java have the concept of signed/unsigned types.  Converting from one to the other does more than just drop the sign.  (So I want to be sure the answer I gave you is correct)
0

Featured Post

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
Hi, Even though I have created this Tutorial on My personal Blog, Some people might not able to find my website, So here i am posting it again Today, from the topic it is very clear that i will be showing you here the very basic usage of how we …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question