Echo $ip_address = $_SERVER['REMOTE_ADDR'];

Hi,

My code:


<?php

echo $ip_address = $_SERVER['REMOTE_ADDR'];

?>

Output:

::1

Why?

Thank you!

Hmmm. I thought I had an answer but when I tried your code it (strangely) exactly what I think you wanted. It showed my ip. So I don’t know what your were outputting but this line shouldn’t have given you what you got. Unless you are running this locally and not from a web site.

Is it something to do with local host? Are you running this on your machine? Just a guess.

you are echoing the boolean of the result of the set method a = b. It correctly set a = b so returned a 1.

Oops, I just noticed ginerjm suggested what I suggested. Silly me. I reckon that’s it.

> you are echoing the boolean of the result of the set method a = b. It correctly set a = b so returned a 1.

That’s definitely not the case.

Thank you guys!

It’s a local host (XAMPP). I expect 127.0.0.1 Why is that? I don’t understand.

http://en.wikipedia.org/wiki/Localhost :

On most computer systems, localhost resolves to the address 127.0.0.1, which is the most-commonly used IPv4 loopback address, and to the IPv6 loopback address ::1.

Thank you very much :slight_smile:

You may want to check for a FORWARD FOR address as many spammers etc will bounce off another ip.

if(isset($_SERVER['HTTP_X_FORWARDED_FOR']) && $_SERVER['HTTP_X_FORWARTDED_FOR'] != '') {
   $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
   $ip_address = $_SERVER['REMOTE_ADDR'];
}

Thank you so much :slight_smile:

I went to the wikipedia reference and I recognize all the words written. But have not the slightest idea what it means.
Can I safely change my code below to replace “127.0.0.1” with “…1” and still have it recognize when I’m on my production host?


    $currentHost = pathinfo($_SERVER['REMOTE_ADDR'],PATHINFO_BASENAME);
echo("<br />currentHost is " . $currentHost); // DEBUG
// determine whether current host is local or production    
    if($currentHost == "127.0.0.1"){		// no place like home
        $theUsername = "root";
        $thePassword = "";
    } else {
        $theUsername = "omitted";
        $thePassword = "omitted"; 
    }
    exit("<br />username {$theUsername} *** password {$thePassword}");

Since this code was written, I’ve purchased a new laptop and downloaded / installed a new XAMPP and what I get is

currentHost is ::1
and the production username and password

try like


<?php

echo $_SERVER['REMOTE_ADDR'];

// or
$ip_address = $_SERVER['REMOTE_ADDR'];
echo $ip_address;
?>

What it means is that localhost in IPv4 is usually 127.0.0.1 In IPv6 it is usually ::1

So, yes you can change your code to use ::1 instead, though you may want to catch both cases.


 in_array( $currentHost, array( '::1', '127.0.0.1' ) )

I would advise against using IPs to detect which environment the application runs in. Instead I would use global variables from Apache. For example put this in the .htaccess file

SetEnv ENVIRONMENT production

or, use a auto_prepend_file on your development machine with something like


<?php
define('ENVIRONMENT', 'developent');

and then in your code


<?php
if (!defined('ENVIRONMENT'))
{
    define('ENVIRONMENT', 'production');
}

// or, an equivalent which some people find harder to read but I personally like it
defined('ENVIRONMENT') || define('ENVIRONMENT', 'production');

So that if you’re on development the ENVIRONMENT will be set to development, and on production where you don’t have the auto_prepend_file ENVIRONMENT won’t be set initially, so you set it to ‘production’.

That way you can use the code on as many machines as you like without having to add all the different IPs :slight_smile:

That is an elegant solution. And, without question, the better one.

If only I trusted my memory. But if I ever buy a new computer and download a new XAMPP version, I’m certain I’d not remember to make this change. And I can see my silly subsequent posts to SitePoint now.

Hence why the htaccess is a good decision, that way you can check it into your code base. So when you change computers, upgrade, whatever, as soon as you pull down your code again, it defaults to the environment you’d want.

If you default to development, be sure that any deployment process you use, change the file to say “production” before copying that file (or alters the file on the production box accordingly).

I have followed this thread because I use similar tests extensively.

I have always tested for $_SERVER[‘SERVER_NAME’] and it has proved to be reliable for quite a number of years:



if ( ! defined('LOCALHOST') ):
   define('LOCALHOST', 'localhost' === $_SERVER['SERVER_NAME'] );
endif;

// test
echo LOCALHOST ? 'localhost' : 'production/online'; die;


I get the impression that using .htaccess to set “SetEnv ENVIRONMENT production” and the include file would require two files with the same name but having different content!!!

Testing and setting the LOCALHOST constant eliminates the problem of overwriting files with the same name but with different content.

Ami I missing the point?

Not necessarily. I typically use phing or my deployment script to alter the .htaccess at the time of deployment, so the actual source code isn’t altered, the deployment process handles it.