admin管理员组

文章数量:1313156

How do I get the inner IP address in my webpage? Can be any language for designing a website (javascript,php,etc.). What I need to do actually, is to make a local web server, and to let the clients in the same wifi network to connect via its shown IP address(192.168.X.X) on a webpage. But I always get 127.0.0.1 in php instead of 192.168.X.X, any ideas?

How do I get the inner IP address in my webpage? Can be any language for designing a website (javascript,php,etc.). What I need to do actually, is to make a local web server, and to let the clients in the same wifi network to connect via its shown IP address(192.168.X.X) on a webpage. But I always get 127.0.0.1 in php instead of 192.168.X.X, any ideas?

Share Improve this question edited Oct 12, 2024 at 20:33 VLAZ 29.1k9 gold badges63 silver badges84 bronze badges asked Mar 2, 2013 at 11:10 Jeffrey NeoJeffrey Neo 3,8092 gold badges28 silver badges30 bronze badges 3
  • 3 Check stackoverflow./questions/3219178/… which contains the same question. – Jensen Commented Mar 2, 2013 at 11:11
  • 1 When you run your page on your localhost you always get your localhost IP: 127.0.0.1 If you upload it to another server you can see your WAN IP – zkanoca Commented Mar 2, 2013 at 11:12
  • Does this answer your question? PHP how to get local IP of system – Stephen Ostermiller Commented Sep 29, 2023 at 11:26
Add a ment  | 

4 Answers 4

Reset to default 6

I solved by the following code, getting the wireless local IP address(192.168.X.X):

$myIP = gethostbyname(trim(`hostname`));
// PHP < 5.3.0
$Local_IP = @gethostbyname(php_uname('n'));

// PHP >= 5.3.0
$Local_IP = @gethostbyname(getHostName());

Most of the methods given above would flawlessly work on a live server but bee a little bit of a menace when you use them on a local server and the least you get is the local IP 127.0.0.1

I am not so sure about Windows or other Linux distros, but with Debian-based distros, you can use exec() to check it directly from the machine.

$serverIP = exec( "hostname -I" );
echo $serverIP;

# output: 192.168.X.X

NOTE: I do not remend using this method on a live server, in fact it's highly advised that you disable the exec function on your live/production server

You just have to read it in

$ip = $_SERVER['SERVER_ADDR'];

If you want to know all data available in $_SERVER use:

print("<pre>\n");
print_r($_SERVER);
print("\n</pre>\n");

$_SERVER contains lots of useful informations. You may also want to check:

$_SERVER['LOCAL_ADDR']

本文标签: