PHP:gethostbyname错误 [英] PHP: gethostbyname bug

查看:198
本文介绍了PHP:gethostbyname错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 gethostbyname()来获取应用程序中域名的IP地址。

I am using gethostbyname() to get the ip address of domains in an application.

还检查了诸如'50 .9.49'之类的无效地址。

In some cases invalid addresses like '50.9.49' are checked also.

echo gethostbyname('50.9.49'); // returns 50.9.0.49

在这种情况下 gethostbyname 应该返回false或未修改的无效ip地址。但是这些函数返回修改后的IP地址 50.9.0.49

In this cases gethostbyname should return false or the unmodified invalid ip address. however the functions returns the modified IP address 50.9.0.49.

看起来像php中的一个错误。快速解决方案似乎是检查无效的数字地址之前,还有其他建议吗?

Looks like a bug in php. The quick fix seems to be to check for invalid numerical addresses before, are there any other suggestions?

推荐答案

PHP的 gethostbyname 实际使用结果底层操作系统的 gethostbyname ,例如,来自Linux的 netdb.h 或Windows' Winsock2.h 。它是那些实际产生返回值的函数,而不是PHP。

PHP's gethostbyname actually uses the results of the underlying OS's gethostbyname, e.g., from Linux's netdb.h or Windows' Winsock2.h. It's those functions that actually produce the return value, not PHP.

/* {{{ php_gethostbyname */
static char *php_gethostbyname(char *name)
{
    struct hostent *hp;
    struct in_addr in;

    hp = gethostbyname(name);

    if (!hp || !*(hp->h_addr_list)) {
        return estrdup(name);
    }

    memcpy(&in.s_addr, *(hp->h_addr_list), sizeof(in.s_addr));

    return estrdup(inet_ntoa(in));
}
/* }}} */

这篇关于PHP:gethostbyname错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆