tags:

views:

1512

answers:

3

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?

+4  A: 
<?php
echo $_SERVER['REMOTE_ADDR'];
?>

You can also do this:

<?php
echo "{$_SERVER['REMOTE_ADDR']}";
?>
sixfoottallrabbit
+3  A: 

No need for "

echo $_SERVER['REMOTE_ADDR'];
Philippe Gerber
+1  A: 

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

inakiabt
Could you briefly explain how your code is better than Philippe's, please.
Masi
Edited: Here explain why http://roshanbh.com.np/2007/12/getting-real-ip-address-in-php.html
inakiabt
That technique might give you a comma separated list of IPs in the case of X-Forwarded-For being used, which you should be aware of if you use this.
Paul Dixon
Good to know... thanks
inakiabt
That technique might give you a piece of shit instead of IP address. It should be $_SERVER['REMOTE_ADDR'] and nothing else
Col. Shrapnel
ooooookaay... :)
inakiabt