Solved

local IP address

Posted on 2004-08-03
20
4,287 Views
Last Modified: 2008-01-09
Is there a variable or method to obtain the ip of the current user that wouldnt return the system loopback ip.
0
Comment
Question by:TaintedGod
  • 6
  • 5
  • 4
  • +3
20 Comments
 
LVL 36

Expert Comment

by:Zyloch
ID: 11710580
<body>
<script language="Javascript">
<!--
if (!document.all) {
   var ip = "" + java.net.InetAddress.getLocalHost().getHostAddress();
   document.write("Your IP address is " + ip);
}
else {
document.write("This doesn't work since you have IE :(");
}
//-->
</script>
</body>

This doesn't work in IE, and needs Java enabled, but it's the only way I think to do it client side
0
 
LVL 13

Expert Comment

by:StormyWaters
ID: 11710668
That's the only way clientside. You could do it serverside very easily.
PHP: $ip = getenv("REMOTE_ADDR");

ASP: Request.ServerVariables("REMOTE_ADDR")
0
 
LVL 13

Expert Comment

by:StormyWaters
ID: 11710675
There are more complicated ways, for instance checking HTTP_X_FORWARDED_FOR to see if they've got a proxy, etc.
0
 
LVL 2

Author Comment

by:TaintedGod
ID: 11710781
OK, I need it to work in IE and notice how i posted this in the javascript section? I need it to get the ip of the client BEFORE the talk to the server, thats why I chose javascript.....
0
 
LVL 13

Expert Comment

by:StormyWaters
ID: 11710847
Notice how I said "That's the only way clientside." and that way is non-IE?

And it'll still have talked to the server, how else does it request the page?

You can still (don't get mad at me, because I'm only listing your possibilities) get the variable in Javascript by echoing it in the proper spot from your serverside language.
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11710901
That's true. When the page loads, you can use server-side to grab the IP address.
0
 
LVL 2

Author Comment

by:TaintedGod
ID: 11711065
Dont you see what i mean, the javascript runs first, THEN the server side crap happens, I need to know the ip with javascript so I can use it with a function....
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11711132
What we're talking about is when the page loads, grab the IP and store it in a Javascript variable if you want. For instance, do this in PHP:

<?php
$ip = getenv("REMOTE_ADDR");
?>
<html>
<head>
<title>Test</title>
<script language="javascript">
<!--
var ip = <?php echo($ip); ?>;
// -->
</script>
</head>
<body>
<script language="javascript">
document.write("The IP Address is"+ip);
</script>
</body>
</html>

Regards,
${Zyloch}
0
 
LVL 13

Expert Comment

by:StormyWaters
ID: 11711593
>>the javascript runs first, THEN the server side crap happens
Quite the reverse, actually.
PHP or another language parses the page before the user ever sees it at all. Then the output of the PHP is sent to the browser for rendering/javascript/whatever.
0
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 2

Author Comment

by:TaintedGod
ID: 11716264
Who said I was using php ?! The only thing serverside happening here is the request for the page! Then I want to javascript to happen and Ill have my server deal with the information posted. I already know how to do this in php, I dont care about that, its pointless, use a serverside language to tell me what the clientside ip is, well, doesnt that just sound logical to you.
0
 
LVL 36

Expert Comment

by:Zyloch
ID: 11716469
Actually, it does... You don't have to use PHP. Most other server-side languages, if not all, have abilities to determine client ip pretty easily. You should determine the ip when the user requests the page and then assign it to a Javascript variable like I did if you want it that way. Why use client-side to determine something that the client never sees? It's the server that sees the IP when the client contacts it.
0
 
LVL 2

Author Comment

by:TaintedGod
ID: 11716724
THere is no point in arguing with you, you obviously dont see my point, which I thought would be straight forward to most. I believe the correct answer to my question is that javascript can not determin the local ip. Thanks for all of your help.
0
 
LVL 13

Expert Comment

by:StormyWaters
ID: 11716771
The original javascript (Zyloch's first comment) is actually using some of the serverside variables available in java to assign the variable to javascript. It is NOT using native clientside javascript to determine the ip address.
So anyway, you can use any serverside language to echo the variable to javascript. But there's no way to actually get it using a clientside solution.
0
 
LVL 2

Author Comment

by:TaintedGod
ID: 11716885
I believe you just restated my ablove comment, thank you.
0
 
LVL 3

Expert Comment

by:andYetAnotherLoginName
ID: 11733209
God, you don't listen. That's bad.

Here's what you want:

user have page with JS that, depending on IP, makes request to server, or not -> possible request to server occurs

Here's what they say:

user requests (#1) the page with JS embedded in it from SOMEWHERE -> THERE, you get an IP on THAT SERVER, which hosts that damn page with JS embedded in it, end "build in" this IP into that page -> you return that goddamn page to user -> user have page with JS that, depending on IP, makes request (#2, now) to server, or not -> possible request #2 to server occurs
0
 
LVL 14

Expert Comment

by:ziffgone
ID: 11743892
"I believe the correct answer to my question is that javascript can not determin the local ip. Thanks for all of your help."

- Correct. :)

If your web hosting company supports serverside includes (SSI), the following will work:

<script>
var ip = '<!--#echo var="REMOTE_ADDR"-->';
if((ip >= country1_low_ip) || (ip <= country1_high_ip)){
     window.location.href = "countrypage1.html";
}
else if((ip >= country2_low_ip) || (ip <= country2_high_ip)){
     window.location.href = "countrypage2.html";
}
etc...

</script>

Or you can try:
<script>
var ip = '<!--#echo var="REMOTE_HOST"-->';

etc...
</script>

None the less, if the user is using a firewall, you may not be able to retrieve their Ip Address no matter which route you take.

Regards....
0
 
LVL 2

Author Comment

by:TaintedGod
ID: 11744752
That is still not what I am looking for, what I wanted was something that would happen on the client side that would put there ip in a var using something like ipconfig...I ALREADY know I can obtain this information from the request IP, I do not want this! No one here seems to be able to understand that. No other information is required now though, I have already scrapped the whole idea.
0
 
LVL 14

Expert Comment

by:ziffgone
ID: 11744819
"I do not want this! No one here seems to be able to understand that."

Everyone here understands that, it's just that everybody here also understands that it can't be done using solely client side scripting. The only way to assign a javascript variable with the user's ip, is by merging both client side scripting and server side scripting, whichever server side language you use. (As both Zyloch's and my examples show, we both assign the javascript variable "ip" with the Ip Address obtained through a short server side language script, from there you could do what you wish with it).

If you are trying to create something that is not run on a web server supporting server side languages, then I'm afraid that it's not likely to happen, unless you can find a programming language supported on the local machine that will do the trick.

Please post a request to delete this question in the EE Support area as I believe you have given up on this idea. And so that no-one else wastes their time here, mostly your time.

I'm sorry there was not an adequate solution for you, but sometimes our scripting languages have absolute limits that can't be worked around. :(

Regards...

0
 

Accepted Solution

by:
CetusMOD earned 0 total points
ID: 11785034
Closed, 125 points refunded.
CetusMOD
Community Support Moderator
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

759 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

20 Experts available now in Live!

Get 1:1 Help Now