✭☭2018-04-25 09:53:37
PHP
✭☭, 2018-04-25 09:53:37

What is the best way to get an IP?

Googled two ways to identify the IP address:

$client  = @$_SERVER['HTTP_CLIENT_IP'];
$forward = @$_SERVER['HTTP_X_FORWARDED_FOR'];
$remote  = @$_SERVER['REMOTE_ADDR'];
 
if(filter_var($client, FILTER_VALIDATE_IP)) $ip = $client;
elseif(filter_var($forward, FILTER_VALIDATE_IP)) $ip = $forward;
else $ip = $remote;

//здесь не понятно зачем нужна собака @, и зачем фильтровать и обязательно ли это делать?

function get_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;
}

Which one is better?
Or suggest your own.
And an additional question - is it possible to use a string with IP as the name of a txt.file?
file_put_contents("$ip.txt");

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Andrey Nikolaev, 2018-04-25
@gromdron

Well, xs, in my opinion, these are not equivalent examples: it is not easier to read the first subjectively, I would prefer the get_ip () function, on the other hand, the second example does not do the same as the first does (for example, it does not validate).

/**
 * Return client ip if find, or null
 * @return string|null
 */
function getUserIp()
{
    static $ip;

    if ( !is_null($ip) )
    {
        return $ip;
    }

    $candidates = [
        @$_SERVER['HTTP_CLIENT_IP'],
        @$_SERVER['HTTP_X_FORWARDED_FOR'],
        @$_SERVER['REMOTE_ADDR']
    ];

    foreach ($candidates as $candidate)
    {
        if ( \filter_var($candidate, \FILTER_VALIDATE_IP) )
        {
            $ip = $candidate;
            break;
        }
    }

    return $ip;
}

var_dump(getUserIp());

S
synapse_people, 2018-04-25
@synapse_people

What are your good features) In terms of the fact that if I send the X_FOWVARDED_FOR header - your site will receive the left ip, not the real one) P.s. not all standard conf web servers cut off this header
Therefore, add at least a check for reverse_proxy_ips

Y
Yan-s, 2018-04-25
@Yan-s

IP in $_SERVER['REMOTE_ADDR'];
Dog (error suppression) in case some of the header was not transmitted (then a non-existent array key will be accessed), but filtering, because there may not be an IP there.
Who will stop you? But if the file itself is not actually needed, it is better to use a database.

A
alexalexes, 2018-04-25
@alexalexes

How to agree with the network administrator, or as is customary on the hosting.
https://habr.com/post/177113/
The dog disables error logging in an array element call.
For example:
If suddenly the $_SERVER array does not contain an element with the 'HTTP_CLIENT_IP' key, then a warning about this will be written to the server log file.
To avoid warnings, you can do this:
or as in the question, using a dog.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question