How to get local IP address using PHP?

One of my friends emailed me to say, “How to get local IP address using PHP?”

The answer is here
[php]

<?php echo sprintf('Local IP address: %s', gethostbyname($_SERVER_NAME)); ?>

[/php]

Here’s how I obtain IP address using PHP:

[php]<?php
echo 'Local IP address: ’ . $_SERVER[‘REMOTE_ADDR’];
?>[/php]

Your answer is incorrect. :wink:

$_SERVER[‘REMOTE_ADDR’] is the correct thing to use for online.

[php]$hosts = gethostbynamel(’’);
echo $hosts[0];[/php]

This will return your IP from localhost.

Sponsor our Newsletter | Privacy Policy | Terms of Service