One of my PHP web applications stopped working today. After I investigated the issues, I noticed that this is a very famous, yet unsolved error. I have no idea why it happens, but I do have a work around solution for it. Basically, this article applies to you if you match all of the following:
- You are using PHP
- Your web applications talk to other servers via domain name (e.g., example.com rather than 22.214.171.124)
- You use XAMPP (instead of native Apache, PHP).
Notice that I am not 100% sure whether this has anything to do with XAMPP. But most of problem I experienced happen on XAMPP platform.
And here are some example problems:
- Getting a file connect using file_get_contents(‘http://example.com/somepage.html’), or CURL etc.
- Sending emails (SMTP server: ‘mail.example.com’)
- Connect to a database server via domain name (‘example.com:3306’)
Why this problem happens?
This problem has nothing to do with your PHP code. In fact, the problem happens when PHP tries to look up the IP address of your domain name. Let’s take a look to the following example. Suppose I have the following code:
$data = file_get_contents('http://example.com/test.html') //process the $data here...
When PHP executes this code, it will try to get the IP address of example.com first, and talk to the server to retrieve the content. This problem happens because PHP is unable to get the IP address of example.com.
Initially, I thought it was my server issues, therefore I tried to ping example.com on the server, i.e.,
and the result looks fine to me. So the problem has nothing to do with the OS / server. Then I run the following code in PHP
$IP = gethostbyname('example.com'); echo $IP;
Normally, I expect to see the IP address of example.com. If it return ‘example.com’, that means PHP is unable to determine the IP address. That explains why the web application stops working.
Solution #1: Restart XAMPP
Try to restart the XAMPP to see whether it resolves the problem or not:
sudo /opt/lampp/lampp stopapache sudo /opt/lampp/lampp startapache
This method aims to resolve the situation that the XAMPP was already started but the network was not available. Restarting the Apache server helps to resolve this problem.
Solution #2: /etc/hosts
Since PHP was unable to lookup the IP address, I decided to give some hints to PHP by editing /etc/hosts:
126.96.36.199 example.com 188.8.131.52 anotherexample.com
This is a quick and easy solution. However, if example.com is moved a different IP address, you will need to update the file. It is pain in long term.
Solution #3: Stop using XAMPP
As I mentioned earlier, I notice that this problem happen in XAMPP environment only. I haven’t experienced this kind of problem with native Apache and native PHP. So I guess it may have something to do with XAMPP.
In fact, it is quite easy to switch from XAMPP to native Apache+PHP+MySQL etc. Native applications give you better performance and reliability and most importantly: the packages get upgraded automatically.
Hope it helps.