glassfish Posted October 19, 2014 Share Posted October 19, 2014 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. Quote Link to comment Share on other sites More sharing options...
Barand Posted October 19, 2014 Share Posted October 19, 2014 That should do it. You get that result because you are viewing a page on localhost from localhost. Quote Link to comment Share on other sites More sharing options...
glassfish Posted October 19, 2014 Author Share Posted October 19, 2014 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? Quote Link to comment Share on other sites More sharing options...
Ch0cu3r Posted October 19, 2014 Share Posted October 19, 2014 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). Quote Link to comment Share on other sites More sharing options...
QuickOldCar Posted October 19, 2014 Share Posted October 19, 2014 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/>"; Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.