如何获取我的系统的 IP 地址

发布于 2024-10-08 01:07:07 字数 113 浏览 0 评论 0原文

如何获取系统的 IP 地址?

我正在使用 $ip_address=$_SERVER[REMOTE_ADDR];,但它返回我的本地主机 IP 地址而不是系统的 IP 地址。

How do I get my system's IP address?

I am using $ip_address=$_SERVER[REMOTE_ADDR];, but it returns my localhost IP address instead of my system's IP address.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

知你几分 2024-10-15 01:07:08

如果您访问本地服务器,则您的本地 IP 地址就是远程 IP。 PHP 正在给你正确的响应。

如果您想知道互联网其他部分的 IP 地址,请访问 Google 并输入“我的 ip 是什么”。

If you are accessing a local server, then your local IP address is the remote IP. PHP is giving you the correct response.

If you want to know your IP address to the rest of the internet, go to Google and type "what is my ip".

孤独患者 2024-10-15 01:07:08

$_SERVER['SERVER_ADDR']

See the php docs for a full list of stuff you might find in the $_SERVER superglobal.

年少掌心 2024-10-15 01:07:08

该函数将确定真实的IP地址:

function ipCheck() {
    if (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    }
    elseif (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_X_FORWARDED')) {
        $ip = getenv('HTTP_X_FORWARDED');
    }
    elseif (getenv('HTTP_FORWARDED_FOR')) {
        $ip = getenv('HTTP_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_FORWARDED')) {
        $ip = getenv('HTTP_FORWARDED');
    }
    else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}

This function will determine the real IP address:

function ipCheck() {
    if (getenv('HTTP_CLIENT_IP')) {
        $ip = getenv('HTTP_CLIENT_IP');
    }
    elseif (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip = getenv('HTTP_X_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_X_FORWARDED')) {
        $ip = getenv('HTTP_X_FORWARDED');
    }
    elseif (getenv('HTTP_FORWARDED_FOR')) {
        $ip = getenv('HTTP_FORWARDED_FOR');
    }
    elseif (getenv('HTTP_FORWARDED')) {
        $ip = getenv('HTTP_FORWARDED');
    }
    else {
        $ip = $_SERVER['REMOTE_ADDR'];
    }
    return $ip;
}
孤千羽 2024-10-15 01:07:08

虽然回复晚了,但我希望它仍然可以帮助别人。我的一个项目需要系统 IP,所以我很快就使用正则表达式废弃了一个自定义函数。它可以在 ubuntu Linux 上运行(从技术上讲应该可以在任何发行版/版本的 Linux 上运行)。

function getLocalIp(){
    $output = shell_exec('/sbin/ifconfig');        
    preg_match("/inet?[[:space:]]?addr:([0-9.]+)/", $output, $matches);
    if(isset($matches[1]))
        $ip = $matches[1];
    else
        $ip = 0;
    return $ip;
}

它需要:

  • 使用 shell_exec 进行系统调用。
  • 使用正则表达式查找内部 ip 地址的值。

如果您碰巧为 Windows 创建了类似的东西或改进了此功能的功能,请与我们其他人分享您的代码。

A late reply but I hope it still might be able to help someone. I needed system ip for a project so I quickly scrapped up a custom function using regular expressions. It works on ubuntu linux (and technically should work on any distro/version of linux).

function getLocalIp(){
    $output = shell_exec('/sbin/ifconfig');        
    preg_match("/inet?[[:space:]]?addr:([0-9.]+)/", $output, $matches);
    if(isset($matches[1]))
        $ip = $matches[1];
    else
        $ip = 0;
    return $ip;
}

It requires:

  • making a system call using shell_exec.
  • using regular expressions to find the value of internal ip address.

If you happen to create something similar for windows or improve the functionality of this function, please share your code with the rest of us.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文