Forums

ASP

This topic is locked

split array

Posted 14 Jul 2005 15:59:52
1
has voted
14 Jul 2005 15:59:52 Michael Behan posted:
I'm getting an Object does not support property or method error on the second line of the following snippet

var ip = Request.ServerVariables("REMOTE_ADDR"
var arr = ip.split("."
var theIP = arr[0]*16777216 X arr[1]*65536 X arr[2]*256 X arr[3]

(X = plus I cant seem to get a plus sign to appear in forum)

it's an asp/javascript page im trying to get the visitors IP address and convert it to a numeric expression.
I cant figure out why I'm getting the error, any ideas?

Replies

Replied 14 Jul 2005 16:30:07
14 Jul 2005 16:30:07 myke black replied:
I assume the code you wrote is in javascript?

You need to change the code to this:


<pre id=code><font face=courier size=2 id=code>
var ip = "&lt;% = Request.ServerVariables("REMOTE_ADDR" %&gt;";
var arr = ip.split(".";

// add check here to make sure ip address is valid - no support for IPv6 here though.
if (arr.length == 4) {
var theIP = (arr[0]*16777216) X (arr[1]*65536) X (arr[2]*256) X arr[3];
} else {
var theIP = 0;
}
</font id=code></pre id=code>

Edited by - mykeblack on 14 Jul 2005 16:31:46
Replied 15 Jul 2005 12:20:18
15 Jul 2005 12:20:18 Michael Behan replied:
the code im working with is actually like this

&lt;%
var ip = Request.ServerVariables("REMOTE_ADDR"
var arr = ip.split("."
var theIP = arr[0]*16777216 X arr[1]*65536 X arr[2]*256 X arr[3]
%&gt;

I need the variable theIP to be an asp variable so I can use it in a recordset later in the page
Replied 15 Jul 2005 13:58:06
15 Jul 2005 13:58:06 myke black replied:
in which case, do this:

&lt;%

ip = Request.ServerVariables("REMOTE_ADDR"
arr = split(ip,"."
if uBound(ip) = 3 then
&nbsp;&nbsp;&nbsp;&nbsp;theIP = arr(0)*16777216 X arr(1)*65536 X arr(2)*256 X arr(3)
end if

%&gt;
Replied 15 Jul 2005 14:19:06
15 Jul 2005 14:19:06 Michael Behan replied:
thanks, but that is asp/vbscript and I'm using asp/javascript
Replied 15 Jul 2005 16:38:49
15 Jul 2005 16:38:49 myke black replied:
ah I see now, in which case this should work:


var ip = new String(Request.serverVariables("REMOTE_ADDR");
var theIP = 0;
if (ip != "" {
&nbsp;&nbsp;&nbsp;&nbsp;var arr = ip.split(".";
&nbsp;&nbsp;&nbsp;&nbsp;if (arr.length == 4) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;var theIP = (parseInt(arr[0])*16777216) X (parseInt(arr[1])*65536) X (parseInt(arr[2])*256) X parseInt(arr[3]);
&nbsp;&nbsp;&nbsp;&nbsp;}
}
Response.Write(theIP);
Replied 15 Jul 2005 17:19:35
15 Jul 2005 17:19:35 Michael Behan replied:
that done it, thanks myke!

Reply to this topic