Jeffrey Neo
Jeffrey Neo

Reputation: 3809

Get the IP address (192.168.X.X, assigned by the wireless router) in my webpage?

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?

Upvotes: 3

Views: 8728

Answers (4)

Abdulbasit
Abdulbasit

Reputation: 790

Most of the methods given above would flawlessly work on a live server but become 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 recommend using this method on a live server, in fact it's highly advised that you disable the exec function on your live/production server

Upvotes: 0

AbdulRahman AlShamiri
AbdulRahman AlShamiri

Reputation: 189

// PHP < 5.3.0
$Local_IP = @gethostbyname(php_uname('n'));

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

Upvotes: 0

Jeffrey Neo
Jeffrey Neo

Reputation: 3809

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

$myIP = gethostbyname(trim(`hostname`));

Upvotes: 6

Jean
Jean

Reputation: 7673

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']

Upvotes: -1

Related Questions