20. 12. 2023 Gianluca Piccolo Development

How to Debug PHP xDebug XD

Sometimes in NetEye 4 it happens that we need to understand why the system behaves in a certain way. Since a lot of the NetEye 4 GUI is still based on PHP, we use the most powerful tool to debug PHP: Xdebug.

Xdebug is an extension of PHP which mainly provides a debugger and profiler. In NetEye 4 we use it often both during the development of new features and for bug-fixes in the remote step-by-step debugging mode. This way you can control code execution from your preferred IDE.

Note: the following procedure assumes your workstation is based on Linux. Since the only tool needed is Ncat which is also distributed on MacOS and Windows, you shouldn’t have problems applying the same method in your environments.

So how does it work? In my case, I set xdebug.start_with_request in the PHP Xdebug configuration to yes, which says to the PHP service, “Hey, enable the debugger for all sessions.” This is less error-prone than activating it with the cookie method. After that I trigger a new session from the browser (I simply reload the page that I want to debug). The PHP service tries to connect to the IDE (all of the most-used IDEs such as Visual Studio Code, Eclipse, WebStorm, Intellij, etc… have an Xdebug integration).
When in the past it didn’t work, I’ve always found the following method useful.

The first thing to do in these cases is to enable the debug log. But that can take time, especially if you only do it once a year XD. So before doing it you should try these quick and easy steps…

Usually the problem is in the connection between the environments. For example, when the application runs in a container, the IDE is in the host machine. This ca mean that at times the PHP service in the container cannot connect to the IDE Xdebug feature. Sometimes it’s because the address specified in the configuration cannot be resolved, while other times it’s the firewall service in the host that doesn’t allow connections to port 9000. The fastest method to figure out if there are connection problems is to listen for incoming connections from the PHP service.

I usually do this with Ncat. On the host machine (Linux) where the IDE is running, I launch an nc -l 9000 command, where 9000 is the default port on which the IDE listens for the PHP service Xdebug connections. Then I trigger a new PHP session from the browser. If nothing is printed from NCAT, we know that the first thing to start looking for is the reason why the connection doesn’t get established.

In this case I suggest checking the following configuration parameters:

  • xdebug.remote_host=host.where.the.ide.runs –> The hostname the IDE is running on
  • xdebug.remote_port=9000 –> The port where the IDE is listening
  • xdebug.remote_connect_back=0 –> This tells PHP to autoconnect to the remote_host address that makes the request. This can be a problem because if this option is enabled, the xdebug.remote_host parameter will be ignored.

To check if the remote host and the remote port are okay, I would also run an easy test. From the host where the PHP service is running, I would try to connect to the host where the IDE is running.
On the IDE’s host, run the nc -l 9000 command. On the host where the PHP service is running, run the following command:

echo "Connection is ok" | nc host.where.the.ide.runs 9000

If on the IDE’s host side the message “Connection is ok” is not printed, then you’ve found at least one of the configuration issues that you need to fix.

Otherwise, the connection is okay and you don’t have an alternative other than enabling the Xdebug logger. Here’s the official guide.

Hope you found this article useful, see you at the next troubleshooting session! 😉

These Solutions are Engineered by Humans

Did you find this article interesting? Does it match your skill set? Programming is at the heart of how we develop customized solutions. In fact, we’re currently hiring for roles just like this and others here at Würth Phoenix.

Gianluca Piccolo
Full Stack Developer at Wuerth Phoenix. I love questioning myself, find new challenges to learn and new adventures to grow up. PHP lover trying to expand my skills studying new languages and tools to improve my professional life.

Author

Gianluca Piccolo

Full Stack Developer at Wuerth Phoenix. I love questioning myself, find new challenges to learn and new adventures to grow up. PHP lover trying to expand my skills studying new languages and tools to improve my professional life.

Leave a Reply

Your email address will not be published. Required fields are marked *

Archive