3

Yes, a similar question was posted and answered correctly. Warning "Do not Access Superglobal $_POST Array Directly" on Netbeans 7.4 for PHP

That post was helpful. but it did not resolve my problem.

I am not getting any OUTPUT in my browser.

I applied all of the options exchanging $_POST with $_SERVER.

My original code:

$user_ip = $_SERVER[ 'REMOTE ADDR'];

My code modified several ways as the other question/ answer suggests

user_ip = filter_input(INPUT_SERVER, 'REMOTE_ADDR');
user_ip = filter_input_array(INPUT_SERVER, 'REMOTE_ADDR');

STILL NO OUTOUT

Background.

I am appending code after code in my index.php as I go thru these tutorials.

I keep commenting out the previous code.

I missed a few comments and at that point I actually DID HAVE OUTPUT

::1

What I expected as output was

127,0,0,1

When I commented the all of the code from the previous tutorial I no longer get any output.

I am going thru a tutorial which is very good but he is using xampp. I am on a Mac and installed amp. Don't know if that matters.

If you want to take a look, navigate to thenewboston.com Tutorials Php Lesson 33 http://thenewboston.org/watch.php?cat=11&number=33 First minute or so in.

Community
  • 1
  • 1
iHaveAQuestion
  • 301
  • 2
  • 5
  • 14
  • 2
    `REMOTE ADDR` (with space) != `REMOTE_ADDR` (with underscore)... `::1` is the IPv6 loopback address, just as 127.0.0.1 is the loopback address for IPv4. – Marc B Feb 19 '14 at 18:37
  • Welcome to Stack Overflow! Please be sure to read the helpful formatting guide that appears when you click the orange question mark in the post editor. – Charles Feb 19 '14 at 18:37
  • @Marc b Sorry. I did have an underscore in REMOTE_ADDR. – iHaveAQuestion Feb 19 '14 at 18:52
  • @marc b admittedly, not new to programming but new to php. Why am I not getting output ? I am learning this using localhost. Why would I even have REMOTE_ADDR in my code. – iHaveAQuestion Feb 19 '14 at 18:55
  • well, your PHP code is broken. You have no `$`, so you're trying to assign the filter results to undefined variables... `$user_ip = $_SERVER['REMOTE_ADDR']`. There's no need to filter that one. It'll be an IP address that's determined by PHP. There's no way for a user to inject malicious data in there. – Marc B Feb 19 '14 at 18:57
  • @marc b okay. Too fast for me. Once I get this stuff I am very intuitive but for now.... I do have an $ in my original line of code. I did not filter to begin with but tried that suggestion from the question/response on this forum. – iHaveAQuestion Feb 19 '14 at 19:02
  • I am trying to figure out formatting for this forum
    hope this works

    no preview options and this makes it hard to comment upon the original post in a formatted fashion
    code is
    $user_ip = $_SERVER['REMOTE_ADDR'];
    echo $string = 'your ip address is: '. $user_ip;
    function echo_ip() {
    $string = 'your ip address is: . '$user_ip;


    output
    `code`your ip address is: ::1
    – iHaveAQuestion Feb 19 '14 at 19:20
  • If you want get variable from $_SERVER, how about use getenv()? – Liao San Kai May 20 '14 at 02:16

2 Answers2

3

When PHP is FastCGI based, filter_input(INPUT_SERVER,… and filter_input_array(INPUT_SERVER… don't return any results!

See the PHP manual entry on filter_input, paying particular attention to the comment by anthony dot parsons

Matthew Slyman
  • 346
  • 3
  • 9
  • Thanks. This caused me some frustration, too. One of MAMP's options on Mac is to run multiple versions of PHP, which requires running PHP as FastCGI... hence NO output for `filter_input_array(INPUT_SERVER)` other than PHP_SELF. – Pagerange Feb 07 '15 at 16:32
0

You can use $_SERVER directly and there is no need of use filter_input.The output ::1 is just because you are running this code on Localhost. If you try your code on server(check you code on RemoteHost) then it will give your cLient's IP. Here is my code for fetch the Client's I.P. See Snapshot Here

   if ($_SERVER['HTTP_CLIENT_IP'])
    {
        $ipaddress = $_SERVER['HTTP_CLIENT_IP'];
    }
    else if($_SERVER['HTTP_X_FORWARDED_FOR'])
    {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else if($_SERVER['HTTP_X_FORWARDED'])
    {
        $ipaddress = $_SERVER['HTTP_X_FORWARDED'];
    }
    else if($_SERVER['HTTP_FORWARDED_FOR'])
    {
        $ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
    }
    else if($_SERVER['HTTP_FORWARDED'])
    {
        $ipaddress = $_SERVER['HTTP_FORWARDED'];
    }
    else if($_SERVER['REMOTE_ADDR'])
    {
        $ipaddress = $_SERVER['REMOTE_ADDR'];
    }
gopal sharma
  • 129
  • 1
  • 6