PHP5:什么 PHP 函数输出用户 IP 地址、平台和浏览器?
在所有返回当前访问者信息/ip 的函数中,只有第一个 一个 似乎输出:
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"];
主要问题:为什么其他函数不输出任何内容?
额外问题:在这方面还有其他很酷的功能,例如输出访问者使用的浏览器和浏览器的功能。平台??同样有用的是获取游客所在的城市、最喜欢的饮料、最喜欢的颜色(以 #RGB 表示)...:) 感谢您的任何建议!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
主要答案:
在服务器上,您只能输出浏览器给您的信息。因此,如果浏览器没有发送“HTTP_X_CLUSTER_CLIENT_IP”信息,服务器和PHP当然无法输出它。
除此之外,$_SERVER 的 文档 不包含类似内容“HTTP_FORWARDED”。
奖励答案:
由于浏览器默认不会发送这些信息,因此您必须通过 JS 获取它们并通过您的 on 发送它们,就像跟踪工具一样(看看 piwik,它是开放代码)或从您获得的其他信息,例如 $_SERVER['HTTP_USER_AGENT'] (请参阅评论)
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)
HTTP_
),$_SERVER[" REMOTE_ADDR"]
因为这些 HTTP 标头是可选的。
肯定
会向您展示所有
注意 get_browser() 函数可以帮助您从 User-Agent 标头中获取更多结构化信息。
HTTP_
in them)$_SERVER["REMOTE_ADDR"]
Because these HTTP headers are optional.
Sure
will show you them all
Note get_browser() function which helps you to get more structured info out of User-Agent header.
使用 $_SERVER['HTTP_USER_AGENT'] 会给你类似的东西:
您可以使用它来确定操作系统和浏览器。
Using $_SERVER['HTTP_USER_AGENT'] will get you something like:
Which you can use to work out the operating system and browser.
尝试一下,得到你想要的:
just try, and get what you want:
首先,您应该使用类似这样的内容:
查看
$_SERVER
变量中的内容:在那里,您会在浏览器发送的 HTTP 标头中找到大量有用的内容。例如:
HTTP_USER_AGENT
:表示浏览器发送的 User-Agent 字符串Mozilla/5.0 (X11; U; Linux x86_64; fr; rv:1.9.2.16) Gecko/20110323 Ubuntu/10.04 (lucid) Firefox/3.6.16
HTTP_ACCEPT_LANGUAGE
:表示浏览器发送的语言首选项fr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
注意:这些只是指示而已!
然后,当涉及到查找用户的 IP 地址时:
REMOTE_ADDR
是通常包含此信息的项目First of all, you should use something like this :
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 browserMozilla/5.0 (X11; U; Linux x86_64; fr; rv:1.9.2.16) Gecko/20110323 Ubuntu/10.04 (lucid) Firefox/3.6.16
HTTP_ACCEPT_LANGUAGE
: indicates the languages preferences sent by the browserfr,fr-fr;q=0.8,en-us;q=0.5,en;q=0.3
Note : those are indications -- only !
Then, when it comes to finding the IP address of a user :
REMOTE_ADDR
is the item that generally contains this information