Help me with some PHP bitches

If you touch your software enough does it become hardware?

Moderator: Forum Moderators

Post Reply
ProfHawking
Zombie
Zombie
Posts: 2101
Joined: February 20th, 2005, 21:31

Help me with some PHP bitches

Post 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?
buzzmong
Weighted Storage Cube
Weighted Storage Cube
Posts: 7167
Joined: February 26th, 2007, 17:26
Location: Middle England, nearish Cov

Post 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.
Dr. kitteny berk
Morbo
Morbo
Posts: 19676
Joined: December 10th, 2004, 21:53
Contact:

Post 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
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post 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.
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Re: Help me with some PHP bitches

Post 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!
Baliame
Tremors Worm
Tremors Worm
Posts: 3491
Joined: October 13th, 2007, 23:43
Location: Hungary

Post 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
Dog Pants
Site Moderator
Site Moderator
Posts: 21653
Joined: April 29th, 2005, 13:39
Location: Surrey, UK
Contact:

Post by Dog Pants »

Good old boolean algebra. Takes me back to the mind-boggling logic circuits I used to create in training.
amblin
Zombie Spanger
Zombie Spanger
Posts: 2663
Joined: October 22nd, 2004, 11:50

Post by amblin »

.
Last edited by amblin on May 5th, 2014, 19:16, edited 1 time in total.
Fear
Zombie
Zombie
Posts: 2032
Joined: August 6th, 2006, 21:45

Post 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.
ProfHawking
Zombie
Zombie
Posts: 2101
Joined: February 20th, 2005, 21:31

Post 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:
wyrd
Polar Bear
Polar Bear
Posts: 275
Joined: March 23rd, 2005, 0:21
Location: in the navy...
Contact:

Post by wyrd »

doesn't that just return 127.0.0.1?
Dr. kitteny berk
Morbo
Morbo
Posts: 19676
Joined: December 10th, 2004, 21:53
Contact:

Post 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
Post Reply