开发者

PHP5: What PHP function outputs Users IP address, Platform and Browser?

Of all these functions that return current visitors info/ip only the first one seems to output:

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"];

Primary Question: Why the other functions dont output anything?

Bonus Question: Are there any other cool functions in this regard for example a function that outputs the visitors used browser & platform?? Also开发者_高级运维 usefull would be to get the visitors city, favourite beverage, favourite color in #RGB... :) Thanks for any suggestions!


  1. It's not functions but array members.
  2. It's not IP address they output but HTTP headers (Note HTTP_ in them)
  3. The only one contains IP address is $_SERVER["REMOTE_ADDR"]

Why the other functions dont output anything?

Because these HTTP headers are optional.

Are there any other cool functions

Sure

print_r($_SERVER);

will show you them all

Note get_browser() function which helps you to get more structured info out of User-Agent header.


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

Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en) AppleWebKit/418 (KHTML, like Gecko) Safari/417.9.3

Which you can use to work out the operating system and browser.


just try, and get what you want:

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


First of all, you should use something like this :

var_dump($_SERVER);

to see what's in the $_SERVER variable : there, you'll find plenty of useful stuff, amongst the HTTP Headers sent by the browser.

Like, for instance :

  • HTTP_USER_AGENT : indicates the User-Agent string sent by the browser
    • For example, mine is 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 the languages preferences sent by the browser
    • For example, mine are : fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3


Note : those are indications -- only !

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


Then, when it comes to finding the IP address of a user :

  • REMOTE_ADDR is the item that generally contains this information
  • But it can be in another item, generally when the user is behing a proxy -- hence the forwarded headers.


Primary Answer: at the server you can output only the information the browser gave you. So if the browser did not send the information "HTTP_X_CLUSTER_CLIENT_IP", the server and of course php are not able to output it.

Besides this, the documentation of $_SERVER does not contain anything like "HTTP_FORWARDED".

Bonus Answer: Due to the fact that the browser does not send those information per default, you have to get them via JS and send them by your on, just like tracking tools do (have a look at piwik, it is open code) or grap them from other information you get, like $_SERVER['HTTP_USER_AGENT'] (see comment)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜