Page 1 of 1

Help me with some PHP bitches

Posted: March 10th, 2008, 11:12
by ProfHawking
Calling all codemonkeys...

I have a webpage that we use for diagnostics at work. We want the webpage to display the users IP address.

I've got it finding the users WAN ip no problemo, but its the LAN ip i would also like to get.
Something like 192.168.x.x thing from inside a network.

Am i correct in thinking that the internal IP never goes past a router? Otherwise, is there any way to make php run an IPCONFIG command on the client, and read an output file?

Posted: March 10th, 2008, 12:22
by buzzmong
Quick google returns:
http://uk3.php.net/manual/en/function.exec.php

And links and comments at the bottom of the page.

I'm only just learning php for a unit, but that looks rather useful.

I don't think IPConfig will write to a file though, you'll probably have to read the app's output from the console and do some string manipulation.

Posted: March 10th, 2008, 12:31
by Dr. kitteny berk

Code: Select all

ipconfig /all > ipcfg.txt
will make ipcfg.txt wherever you run it from.


so in console

Code: Select all

C:\>ipconfig /all > ipcfg.txt
will make an ipcfg.txt in the root of C

Posted: March 10th, 2008, 13:57
by Fear
The IP range you specified will never get routed by the internet. It is a reserved range with exactly that purpose.

I'm guessing a bit here, but I am imagining you've got various machines with 192.168.x.x/16 or similar, and then a shared connection to the internet using Network Address Translation. In this case, the internal IPs will never be seen by anything outside of your LAN.

If the web server was on an Intranet, this would of course be different.

At the end of the day, it is only possible to see the IP the machine chooses to use to communicate, so if a machine had two IPs, you would only see the one IP it chooses to use.

I would instead ask the user to run this in the run bar:

Code: Select all

ipconfig /all > "%USERPROFILE%\desktop\ipconfig.txt"
And email you the text file that appears on their desktop.

Re: Help me with some PHP bitches

Posted: March 10th, 2008, 14:06
by Fear
ProfHawking wrote:is there any way to make php run an IPCONFIG command on the client, and read an output file?
PHP is server side, it wont run anything on the client.

Your options here are to write an ActiveX application (IE only), or write a java app - but you are going to have to contend with a shit load of security blocks. Java by default, and rightly so, allows fuck all access to the file system. You can agree to let it tho, I think.

Imagine if a website asked the client to run FDisk instead of IPConfig!

Posted: March 10th, 2008, 14:34
by Baliame
This may not be right but you will get the bitwise AND result of his local IP and his subnet mask, so if he has the rather usual local IP of 192.168.1.72 and the rather usual subnet mask of 255.255.255.0 then what you get is 192.168.1.0/24 (http://en.wikipedia.org/wiki/Subnet_Mask)

Bitwise AND explained:
255.255.255.0 = 11111111.11111111.11111111.00000000
192.168.1.0=11000000.10101000.00000001.01001000

Now we check at which places are bits both "1".

Code: Select all

11000000.10101000.00000001.01001000 AND
11111111.11111111.11111111.00000000
-----------------------------------
11000000.10101000.00000001.00000000

Posted: March 10th, 2008, 15:24
by Dog Pants
Good old boolean algebra. Takes me back to the mind-boggling logic circuits I used to create in training.

Posted: March 10th, 2008, 16:02
by amblin
.

Posted: March 10th, 2008, 16:56
by Fear
Baliame wrote:This may not be right but you will get the bitwise AND result of his local IP and his subnet mask, so if he has the rather usual local IP of 192.168.1.72 and the rather usual subnet mask of 255.255.255.0 then what you get is 192.168.1.0/24 (http://en.wikipedia.org/wiki/Subnet_Mask)

Bitwise AND explained:
255.255.255.0 = 11111111.11111111.11111111.00000000
192.168.1.0=11000000.10101000.00000001.01001000

Now we check at which places are bits both "1".

Code: Select all

11000000.10101000.00000001.01001000 AND
11111111.11111111.11111111.00000000
-----------------------------------
11000000.10101000.00000001.00000000
<3 boolean algebra - the best of all the maths.

The IP is just 4 bytes, in dotted decimal, and the /24 (or whatever) is just the sum of number of consecutive 1's in the binary subnet mask.

If you bitwise AND the IP and subnet, you get the network portion.

What this then means is any address starting the same as the network portion is routed directly (LAN) - and anything different is sent to the router (your 'default gateway' in windows) to be routed from there, probably around the Internet.

This is all academic when it gets to your router though, as that then gives it an entirely different source IP to that of your WAN interface, and tracks the tcp/ip session so it can send any replies it receives back to the correct internal PC.

In short, you're not gonna get the "LAN IP" without running an application on the "LAN PC" running with enough security rights to read the IP configuration and then transmit the results to some web server somewhere.

Posted: March 10th, 2008, 18:49
by ProfHawking
Ok, shame PHP cant run local things, like ipconfig then. I suppose it would be a massive security issue if it could.

And agreed, behind a router (thats doing its job) the lan ip is not going to get out. But that doesnt matter, as its only the client that needs to see it.

I have got it working with a bit of javascript:

Code: Select all

<script>
var address = java.net.InetAddress.getLocalHost();
document.write("Your IP is: " + address);
</script>
Unfortunately this only works in firefox/opera. In IE6 or 7 it fails miserably. Unfortunately that seems to be the browser of choice for our client minions. :cry:

Posted: March 10th, 2008, 18:54
by wyrd
doesn't that just return 127.0.0.1?

Posted: March 10th, 2008, 19:15
by Dr. kitteny berk
wyrd wrote:doesn't that just return 127.0.0.1?
I half expected that, but it works fine :likesitall: most handy