SitePoint Sponsor

User Tag List

Results 1 to 18 of 18
  1. #1
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

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

    Hi,

    My code:

    Code:
    <?php
    
    echo $ip_address = $_SERVER['REMOTE_ADDR'];
    
    ?>
    Output:

    ::1
    Why?

    Thank you!

  2. #2
    SitePoint Enthusiast
    Join Date
    Jul 2013
    Location
    Voorheesville NY USA
    Posts
    88
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    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.

  3. #3
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Is it something to do with local host? Are you running this on your machine? Just a guess.

  4. #4
    SitePoint Member
    Join Date
    Aug 2007
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you are echoing the boolean of the result of the set method a = b. It correctly set a = b so returned a 1.

  5. #5
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    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.

  6. #6
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you guys!

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

  7. #7
    SitePoint Wizard
    Join Date
    Apr 2002
    Posts
    2,307
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 8Observer8 View Post
    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.

  8. #8
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you very much

  9. #9
    SitePoint Guru bronze trophy
    Join Date
    Feb 2013
    Posts
    730
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    You may want to check for a FORWARD FOR address as many spammers etc will bounce off another ip.
    PHP Code:
    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'];


  10. #10
    SitePoint Zealot 8Observer8's Avatar
    Join Date
    Jul 2013
    Location
    Russia
    Posts
    105
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you so much

  11. #11
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by johnyboy View Post
    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.
    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?

    Code:
        $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

  12. #12
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,099
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    try like
    PHP Code:
    <?php

    echo $_SERVER['REMOTE_ADDR'];

    // or
    $ip_address $_SERVER['REMOTE_ADDR'];
    echo 
    $ip_address;
    ?>
    What I lack in acuracy I make up for in misteaks

  13. #13
    SitePoint Evangelist
    Join Date
    Oct 2005
    Location
    Michigan, USA
    Posts
    434
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    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.
    PHP Code:
     in_array$currentHost, array( '::1''127.0.0.1' ) ) 
    - Robert

  14. #14
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,070
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    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 Code:
    <?php
    define
    ('ENVIRONMENT''developent');
    and then in your code

    PHP 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
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  15. #15
    SitePoint Addict
    Join Date
    Oct 2005
    Posts
    288
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ScallioXTX View Post
    or, use a auto_prepend_file on your development machine with something like

    PHP Code:
    <?php
    define
    ('ENVIRONMENT''developent');
    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.

  16. #16
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Grnadpa View Post
    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).

  17. #17
    SitePoint Mentor bronze trophy
    John_Betong's Avatar
    Join Date
    Aug 2005
    Location
    City of Angels
    Posts
    1,833
    Mentioned
    73 Post(s)
    Tagged
    6 Thread(s)
    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:
    PHP Code:

    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?
    Learn how to be ready for The New Move to Discourse

    How to make Make Money Now with a *NEW* look

    Be sure to congratulate Patche on earning Member of the Month for July 2014

  18. #18
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,131
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by John_Betong View Post
    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!!!
    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.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •