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
4 Answers
Reset to default 6I 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']
本文标签:
版权声明:本文标题:javascript - Get the IP address (192.168.X.X, assigned by the wireless router) in my webpage? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741935869a2405849.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论