How to print out an associative array item?

I would like to know how you can echo the ip-address of the user such that you can use it in your login cookie.

My code

<?php
         echo "$_SERVER['REMOTE_ADDR']";
?> 

I run it and I get in Firefox

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /var/www/file7.php on line 2

How can you get the IP-address by PHP?

Upvotes: 0

Views: 1784

Answers (3)

inakiabt
inakiabt

Reputation: 1963

This is the best way to get an IP:

function ip()
{
    if (!empty($_SERVER['HTTP_CLIENT_IP']))
    {
        $ip = $_SERVER['HTTP_CLIENT_IP'];
    } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR']))
    {
        $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
    }
    else
    {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;            
}

echo ip();

Edit: Source: http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html

Upvotes: 1

Joseph Mansfield
Joseph Mansfield

Reputation: 110698

<?php
echo $_SERVER['REMOTE_ADDR'];
?>

You can also do this:

<?php
echo "{$_SERVER['REMOTE_ADDR']}";
?>

Upvotes: 4

Philippe Gerber
Philippe Gerber

Reputation: 17846

No need for "

echo $_SERVER['REMOTE_ADDR'];

Upvotes: 3

Related Questions