How to learn ip of the visitor of the website

How to learn ip of the visitor of the website

Digital Hits: 74

Most often the IP address of the visitor of the website is used for its identification. But also besides by means of IP it is possible to obtain much additional information on the visitor - for example, to recognize his Internet service provider and geographical location. In practice most often for extraction of the IP addresses from headings of the requests sent by the browser use scripts in the server PHP language.

It is required to you

Instruction

1. Use the embedded function of getenv of the PHP language for reading the IP addresses from a superglobal array of variables of an environment. In the simplest case will be to read enough the variable with the name REMOTE_ADDR. The corresponding fragment of the PHP code can look so: $userIP = getenv ('REMOTE_ADDR');

2. Check except the REMOTE_ADDR variable sent in request and the HTTP_VIA and HTTP_X_FORWARDED_FOR variables. If the visitor uses the proxy server, then the intermediate address should be written in both variables - both in HTTP_VIA, and in REMOTE_ADDR. In this case to learn real IP of the visitor it is possible to try through HTTP_X_FORWARDED_FOR - the proxy server should place a source address in it. However it becomes not always, and the user has an opportunity to select the "opaque" proxy server which does not transfer initial IP of the visitor who sent request. Anyway you should involve in the code at most of ways to receive the initial IP address, having added also verification of the HTTP_CLIENT_IP variable.

3. Integrate consecutive verification of three variables of an environment which may contain the initial IP address of the visitor in one line of the PHP code. It is possible to make it, for example, so: $userIP = getenv ('HTTP_CLIENT_IP') of OR of $userIP = getenv ('HTTP_X_FORWARDED_FOR') of OR of $userIP = getenv ('REMOTE_ADDR');

4. Delete excess characters and other "garbage" which can get to variable environments from the received IP value. It is possible to make it, for example, having involved the built-in TRIM and preg_replace PHP functions: $userIP = TRIM (preg_replace ('#^ ([^]+) (. *) ?#', '$1', $userIP));

5. Integrate all code in the user function to have an opportunity to contact it, but not to repeat a line of check and cleaning again in different parts of PHP scripts. For example, so: FUNCTION getUserIP () {
$userIP = getenv ('HTTP_CLIENT_IP') of OR of $userIP = getenv ('HTTP_X_FORWARDED_FOR') of OR of $userIP = getenv ('REMOTE_ADDR');
RETURN TRIM (preg_replace ('#^ ([^]+) (. *) ?#', '$1', $userIP));
}

Author: «MirrorInfo» Dream Team

Print