Jump to content

Printing the IP Address From Which the Site is Viewed on Screen?


glassfish

Recommended Posts

This script ...

<?php

    echo "Your IP Address: <br/>";
    print_r($_SERVER['REMOTE_ADDR']);

?>

... prints:

::1

I am looking to have the IP address from which the site is viewed printed on screen. What is the issue, here, with the script I have?

 

I have been trying this on localhost and I am using XAMPP.

I saw this in the PHP documentation:

SERVER_ADDR    127.0.0.1
SERVER_NAME    localhost
SERVER_SOFTWARE    Apache/2.2.22 (Win64) PHP/5.3.13
SERVER_PROTOCOL    HTTP/1.1 
...
REMOTE_ADDR    127.0.0.1 

Is there a way to get specifically the remote address in this way myself?

That is what $_SEVER['REMOTE_ADDR'] is for. It returns the IP address to the remote user.

 

You are getting  ::1  because you are accessing your server locally using IPv6. To see 127.0.0.1 as the IP you need to use IPv4.

 

To use the IPv4 either uncomment   ::1 localhost   from your hosts file (or disable IPv6 on your Network Adapter - not recommended).

or add an if and replace to 127.0.0.1

if($_SERVER['REMOTE_ADDR'] == "::1" || $_SERVER['REMOTE_ADDR'] == "localhost"){
$ip = "127.0.0.1";
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}

echo "Your IP Address: ".$ip."<br/>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.