Solved

IP Address to Country in Coldfusion

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
This is an updated version of a post made on my blog over 3 years ago. It is unfortunately, still very relevant as we continue to see both SQLi (SQL injection) and XSS (cross site scripting) attacks hitting some of the most recognizable website and …
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now