PHP: gethostbyname error

I use gethostbyname() to get the IP address of domains in the application.

In some cases, invalid addresses such as "50.9.49" are also checked.

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

In this case, gethostbyname should return false or an unmodified invalid IP address. however, functions return the changed IP address 50.9.0.49 .

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

+6
source share
2 answers

PHP gethostbyname actually uses the results of the base gethostbyname OS, for example, from Linux netdb.h or Windows' Winsock2.h . These are functions that actually produce a 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)); } /* }}} */ 
+7
source

This seems to be an undocumented feature for working IP addresses. As mentioned in the comments on your question, ping 50.9.49 on Windows is actually ping 50.9.0.49 . If you enter the address as abd , it will automatically add zero as c : ab0.d If you just type ad , two zeros are inserted: a.0.0.d

This has been tested with both Windows 7 and Debian Linux.

+3
source

Source: https://habr.com/ru/post/915926/


All Articles