php - 如何打印您所在网站的“公共”IP 地址?
问题描述
到目前为止,我已经尝试过:
echo gethostbyname(gethostname());
但这给了我“本地”IP(不确定这是否是正确的术语)127.0.0.1
,而我想要“公共”IP 地址。
例如,对于我151.101.193.69
在使用网站服务https://ipinfo.info时获得的网站 stackoverflow.com ......这就是我正在寻找的。
解决方案
使用此代码,
function getIPAddress() {
//whether ip is from the share internet
if(!empty($_SERVER['HTTP_CLIENT_IP'])) {
$ip = $_SERVER['HTTP_CLIENT_IP'];
}
//whether ip is from the proxy
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
}
//whether ip is from the remote address
else{
$ip = $_SERVER['REMOTE_ADDR'];
}
return $ip;
}
推荐阅读
- python - 无法使用python访问json文件
- python - 如何在 Python 中运行进程?
- mongodb - MongoDB查询将时间戳与嵌套对象时间戳进行比较
- http - 从 localhost 移动到托管站点时诊断“请求发起者链”中的附加重定向
- node.js - 通过 IIS 代理向 nodejs https 服务器发送带有 http post 请求的大数据时出现错误 400.6
- android - 如何启用 Compose 快餐栏的滑动关闭行为
- sql - SQL 计算 Redshift 中的总持续时间
- graphviz - 修复graphviz中簇的位置
- c - AFL-fuzz 没有发现任何崩溃
- r - 计算一个值出现的次数并将结果添加到列中