Page 1 of 1

Getting IP address

Posted: Mon Mar 08, 2010 1:26 am
by manoj_hicool
How can I print my system's local IP address in browser?

I tried the below methods and I did not get that.
print_r($_SERVER);
print "<br><br>";
print_r( $HTTP_SERVER_VARS);

Re: Getting IP address

Posted: Mon Mar 08, 2010 2:21 am
by pbs
User $_SERVER['SERVER_ADDR'];

Re: Getting IP address

Posted: Mon Mar 08, 2010 2:44 am
by manoj_hicool
I tried this and it is printing,

print "IP :: ".$_SERVER['SERVER_ADDR'];
IP :: 192.168.1.12

But my local IP address is in 192.168.8.102 .

Re: Getting IP address

Posted: Mon Mar 08, 2010 3:38 am
by pbs
Try $_SERVER['REMOTE_ADDR'];

Re: Getting IP address

Posted: Mon Mar 08, 2010 4:29 am
by manoj_hicool
I tried both the methods,
print "IP :: ".$_SERVER['SERVER_ADDR'];
print "IP1 :: ".$_SERVER['REMOTE_ADDR'];

It prints the result as,
IP :: 192.168.1.12
IP1 :: 192.168.1.10

It's not my system's local IP (192.168.8.102).

Re: Getting IP address

Posted: Mon Mar 08, 2010 4:40 am
by VladSun
$_SERVER['LOCAL_ADDR'] ?

Re: Getting IP address

Posted: Mon Mar 08, 2010 4:57 am
by manoj_hicool
It doesn't print anything to me.

$_SERVER['LOCAL_ADDR']

Re: Getting IP address

Posted: Mon Mar 08, 2010 5:02 am
by VladSun
Is it Windows (version) or Linux?

Re: Getting IP address

Posted: Mon Mar 08, 2010 5:18 am
by manoj_hicool
It is linux system
Linux version 2.6.26-2-686 (Debian 2.6.26-17lenny2) (dannf@debian.org) (gcc version 4.1.3 20080704 (prerelease) (Debian 4.1.2-25)) #1 SMP Fri Aug 14 01:27:18 UTC 2009

Re: Getting IP address

Posted: Mon Mar 08, 2010 5:26 am
by VladSun
What's the output of:

Code: Select all

ifconfig
?

Re: Getting IP address

Posted: Mon Mar 08, 2010 5:32 am
by manoj_hicool
/sbin/ifconfig
eth0 Link encap:Ethernet HWaddr 00:1D:92:DE:49:9B
inet addr:192.168.8.20 Bcast:192.168.8.255 Mask:255.255.255.0
inet6 addr: fe80::21d:92ff:fede:499b/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4281176819 errors:0 dropped:3002 overruns:0 frame:0
TX packets:295399131 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:1770624014 (1.6 GiB) TX bytes:2600959972 (2.4 GiB)
Interrupt:23 Base address:0xdc00

lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:16436 Metric:1
RX packets:3520722 errors:0 dropped:0 overruns:0 frame:0
TX packets:3520722 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:0
RX bytes:1208552913 (1.1 GiB) TX bytes:1208552913 (1.1 GiB)

Re: Getting IP address

Posted: Mon Mar 08, 2010 5:33 am
by VladSun
192.168.1.12/192.168.1.10 and 192.168.8.102 seem to be in different network segments.

Are you sure there isn't a router between your PC (192.168.1.10), which masks (DMZ, SNAT or port forwarding) the web server IP address (192.168.8.102) by replacing it with its own IP address - 192.168.1.12 ?

PS: :oops: Never mind, I think the $_SERVER['SERVER_ADDR'] has nothing to do with it ...
Strange...

Re: Getting IP address

Posted: Mon Mar 08, 2010 5:40 am
by VladSun

Code: Select all

echo gethostbyname($_SERVER['SERVER_NAME']);
?

Re: Getting IP address

Posted: Mon Mar 08, 2010 5:48 am
by manoj_hicool
It prints the IP as,
echo gethostbyname($_SERVER['SERVER_NAME']);

192.168.1.12