Click to See Complete Forum and Search --> : Getting A Visitor's IP Address
chestertb
10-09-2003, 09:48 AM
Does anyone know a quick and easy way I can get a visitor's IP address?
My utopia would be a simple call I could make from html or javascript to, say, php on the server which would then return a value into a JavaScript variable.
Note that I tried
<server>
var ip=request.ip
</server>
but a) I barely understand client-side javascript, so it's fair to assume I got the server side code/syntax completely wrong
and b) I am not sure if my host provider allows me to run server-side java scripting.
Does anyone have a simple solution?
Thanks
IB
ps... vis-a-vis the posts today about doing a search before posting a question... on this, I'd love to, except that "ip" is less than the mandatory 4 characters.
sciguyryan
10-09-2003, 10:21 AM
php version:
<? echo $_SERVER['REMOTE_ADDR'] ?>
or the sort of JS version:
SCRIPT LANGUAGE="JAVASCRIPT" TYPE="TEXT/JAVASCRIPT">
<!--
var ip = '<!--#echo var="REMOTE_ADDR"-->';
window.alert("Your IP address is "+ip);
window.defaultStatus = "Your IP address is "+ip;
document.write("<TITLE>Your IP address is "+ip+"</TITLE>");
//-->
</SCRIPT>
i think those will work.
chestertb
10-09-2003, 10:40 AM
Thanks Ryan,
The second JS code just returns
Your ip address is <!--#echo var="REMOTE_ADDR"-->, which is odd, because I seem to remember using that once before and it worked. Ah well... the PHP route is probably a better one.
How do I get a variable I can use in Javascript out of the PHP example?
Thanks
IB
sciguyryan
10-09-2003, 10:51 AM
i have no idea how to export variables from PHP into JS as my specialities lie in JSP, JS and VBScript - i can do it for them but, perhaps you should post that in the PHP forum?
hang on i remember a script that does some thing like that give me a minute>.......
sciguyryan
10-09-2003, 10:55 AM
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
if ((navigator.appVersion.indexOf("4.") != -1) && (navigator.appName.indexOf("Netscape") != -1)){
ip = "" + java.net.InetAddress.getLocalHost().getHostAddress();
}
//-->
</script>
that stores the ip so you can use it.
sciguyryan
10-09-2003, 10:56 AM
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
if ((navigator.appVersion.indexOf("4.") != -1) && (navigator.appName.indexOf("Netscape") != -1)){
ip = "" + java.net.InetAddress.getLocalHost().getHostAddress();
}
//-->
</script>
that stores the ip so you can use it but, it only works on netscape and if java is enabled - i will see if i can find a better one for you.
Khalid Ali
10-09-2003, 10:58 AM
the php code posted should work,you may need to change the html file extension to .php
chestertb
10-09-2003, 12:07 PM
Thanks Ryan.
Really appreciate you taking the time to help.
Ian.