Solved

IP Address to Country in Coldfusion

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

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Entering multiple email addresses 4 79
Coldfusion retrieve dynamic values 1 42
Coldfusion RegEx 8 75
How to add a blog at the end of my web pages - using Cold Fusion 3 49
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…
The technique is by far very Simple! How we can export the ColdFusion query results to DOC file?  Well before writing this I researched a lot in Internet but did not found a good Answer anyways!  So i thought now i should share my small snippet w…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

740 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