Solved

IP Address to Country in Coldfusion

Posted on 2013-01-10
5
295 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 500 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Hi, I will be creating today a basic tutorial on how we can create a Mail Custom Function and use it where ever we want. The main advantage about creating a custom function is that we can accommodate a range of arguments to pass to the Function and …
I spent nearly three days trying to figure out how incorporate OAuth in Coldfusion for the Eventful API. Hopefully, this article will allow Coldfusion Programmers to buzz through the API when they need to. Basically, what this script does is authori…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

632 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