PHP5: what PHP function displays the IP address of users, platform and browser?

Of all these functions that return current info / ip visitors, only first is deduced:

echo $_SERVER["REMOTE_ADDR"]; echo $_SERVER["HTTP_X_FORWARDED"]; echo $_SERVER["HTTP_X_CLUSTER_CLIENT_IP"]; echo $_SERVER["HTTP_FORWARDED_FOR"]; echo $_SERVER["HTTP_FORWARDED"]; 

Main question: Why do other functions not display anything?

Bonus question: Are there any other interesting features in this regard, for example, a function that displays visitors used by the browser and platform? It would also be useful to get the city of visitors, your favorite drink, your favorite color in #RGB ... :) Thanks for any suggestions!

+4
source share
5 answers
  • It does not work, but contains array elements.
  • not an IP address , they output, but HTTP headers (note HTTP_ )
  • The only one containing the IP address is $_SERVER["REMOTE_ADDR"]

Why don't other functions output anything?

Because these HTTP headers are optional.

Are there any other cool features

Sure,

 print_r($_SERVER); 

will show you all of them

Check out the get_browser () function , which helps you get more structured information from the User-Agent header.

+5
source

Using $ _SERVER ['HTTP_USER_AGENT'], you will get something like:

Mozilla / 5.0 (Macintosh; U; PPC Mac OS X; ru) AppleWebKit / 418 (KHTML, e.g. Gecko) Safari /417.9.3

What you can use to develop the operating system and browser.

+1
source

just try and get what you want:

 echo "<pre>"; print_r($GLOBALS); echo "</pre>"; 
+1
source

First of all, you should use something like this:

 var_dump($_SERVER); 

to find out what's in the $_SERVER : there you will find a lot of useful stuff among the HTTP headers sent by the browser.

Such as:

  • HTTP_USER_AGENT : indicates the User-Agent string sent by the browser
    • For example, my Mozilla/5.0 (X11; U; Linux x86_64; fr; rv:1.9.2.16) Gecko/20110323 Ubuntu/10.04 (lucid) Firefox/3.6.16
  • and HTTP_ACCEPT_LANGUAGE : indicates language preferences sent by the browser
    • For example, mine: fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3


Note: these instructions are only!

  • They can be faked (and even contain malicious data).
  • They cannot be there.


Then, when it comes to finding the user's IP address:

  • REMOTE_ADDR is an element that usually contains this information.
  • But it can be in another element, usually when the user runs a proxy server, therefore, forwarded headers.
+1
source

The main answer: on the server you can display only the information that the browser provided to you. Therefore, if the browser did not send the "HTTP_X_CLUSTER_CLIENT_IP" information, the server and, of course, php will not be able to output it.

In addition, the documentation in $ _SERVER does not contain anything like "HTTP_FORWARDED".

Bonus answer: Due to the fact that the browser does not send this information by default, you should receive them via JS and send them using on, like the tracking tools (look at piwik, this is open source code) or grap them from other information, which you get, for example $ _SERVER ['HTTP_USER_AGENT'] (see comment)

+1
source

Source: https://habr.com/ru/post/1347346/


All Articles