469,336 Members | 5,746 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,336 developers. It's quick & easy.

gathering IP addresses in form submissions

Hello,

I'm an HTML coder, and am learning how to integrate JS and PHP into form
functions.

I'm trying to engineer a form that will return the user's IP address as a
hidden input field.

I found a code snippet to capture the visitor's IP as such:

==================
<SCRIPT>
var ip = new java.net.InetAddress.getLocalHost();
var ipStr = new java.lang.String(ip);
document.writeln(ipStr.substring(ipStr.indexOf("/")+1));
</SCRIPT>
========================

and I tried to use the variable (both as "ip" and as "ipStr"), but with the
resulting form return yeilding "undefined".

my hidden field is generated by a Document.write statement as such:

===========================
<script type="text/javascript">
document.write ('<input type="hidden" name="ip_number" value="' + ipStr +
'">');
</script>
===========================

thanks for any help in solving this!

Tam
Mar 6 '07 #1
2 3172
Lee
Tamarin said:
>
Hello,

I'm an HTML coder, and am learning how to integrate JS and PHP into form
functions.

I'm trying to engineer a form that will return the user's IP address as a
hidden input field.

I found a code snippet to capture the visitor's IP as such:

==================
<SCRIPT>
var ip = new java.net.InetAddress.getLocalHost();
var ipStr = new java.lang.String(ip);
document.writeln(ipStr.substring(ipStr.indexOf( "/")+1));
</SCRIPT>
========================
That will only work in a few browsers, and what it will tell you
is the IP address that my PC uses in my local network. Unless
you're working on a company intranet, it's not useful to you.
There are thousands of other people using the same IP address.

Your PHP code on the server side already knows what it thinks
my IP address is, anyway. That's a different IP from what my
browser would tell you, and just about as useless. Depending
on which computer I use, it may be an address that changes
every time I log in, or it may be a proxy address that I share
with many other people.
>my hidden field is generated by a Document.write statement
Don't do that. Create the hidden field in normal HTML and
simply use javascript to set its value:

document.forms["myFormName"].elements["ip_number"].value=ipStr;
--

Mar 6 '07 #2
Lee said the following on 3/6/2007 10:20 AM:
Tamarin said:
<snip>
>my hidden field is generated by a Document.write statement

Don't do that. Create the hidden field in normal HTML and
simply use javascript to set its value:

document.forms["myFormName"].elements["ip_number"].value=ipStr;
Or, let PHP generate it with the IP Address it thinks is right and then
the form can get submitted and PHP can think the client gave it to them :-)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Mar 6 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Steve Holden | last post: by
3 posts views Thread by Mark Michel | last post: by
6 posts views Thread by nick4soup | last post: by
reply views Thread by Toby Inkster | last post: by
15 posts views Thread by Rainman | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.