Link to home
Start Free TrialLog in
Avatar of Mark Drelinger
Mark DrelingerFlag for United States of America

asked on

javascript to put IP into text field not working

javascript error
Not sure if I'm just being silly, but can't seem to get this script to work

function GetIP()
{
    var strIP
      strIP = Request.ServerVariables("HTTP_X_FORWARDED_FOR");
    If strIP = "" Then strIP = Request.ServerVariables("REMOTE_ADDR");
    IP = strIP;
      document.form1.User_IP.value = IP;

}
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

you can't get IP address via client side javascript. this got to be done at server side instead.
Hi Mark,

The code you have written looks like classic ASP 3.0 code. It executes on server and then sets IP in a form. If you post the entire code I will be able to tell you why it does not populate on the form.

Regards,
Chinmay.
Accessing headers from Javascript is possible + likely more complex than you imagine.

How you do this relates to many factors.

https://stackoverflow.com/questions/220231/accessing-the-web-pages-http-headers-in-javascript provides a good overview of various methods of header access.
Avatar of Mark Drelinger

ASKER

I might have been overthinking it since I only needed internal IP address.
This seemed to work
<input name="User_IP" type="hidden" id="User_IP" value="<%=Request.ServerVariables("REMOTE_ADDR")%>">
Is this a bad practice ?
Is this a bad practice ?
any particular reason you want to show the IP address in your page?
ASKER CERTIFIED SOLUTION
Avatar of Mark Drelinger
Mark Drelinger
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks to all - I split the points because everyone was helpful...