I'm working on a PHP application. There is a requirement to find IP's by hostname and i have to resort to gethostbynamel for that. Looks like there is no other way.

I was wondering if there are any security implications related to this. I am validating the hostname given as input from end-user by using regular expressions.

I have two options:

  1. When user submits a form with hostname, validate it and use gethostbynamel
  2. When user submits a form with hostname, validate it store it in database and use cron job which runs gethostbynamel

Will there be any difference in the above methods?

Security is top priority for the application.

  • What is the reasoning for getting the IP? Is it like a reverse lookup? What types of hosts are you expecting? Commented Sep 20, 2013 at 12:17
  • @mikemackintosh In my application, licensing is based on IP's. So if user enters hostname, i need to get IP's to calculate licenses consumed.
    – david
    Commented Sep 20, 2013 at 12:22

1 Answer 1


The php function gethostbynamel is a wrapper over the native OS gethostbyname function.
The implementation can be seen here.

So the same vulnerabilities that apply to the gethostbyname syscall will apply to your code. I don't see any existing gethostbyname vulnerabilities that would bother me.

So basically as long as you are validating the hostnames accurately, you should be fine.
See this SO answer and this text for help on regex.

Amongst the two options you posted, they don't make any difference so far as the security implications are concerned. gethostbyname is a network call, so there is a non-negligible time associated with it. This can be one of the differentiators why you'd choose to use one over the other.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .