Jump to content

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


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/>";
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.