Getting Xdebug, Xampp and Netbeans to work together in Windows can be a tricky thing and I want to address that here. These instructions will get debugging working all on the same PC and IP.
A symptom of a PC that is not setup correctly is this Netbeans Socket Exception error. You get this error when Netbeans cannot communicate with Xdebug and is usually because you are running them both on the same computer and therefore IP so the traffic routing is getting mixed up. It could also because something else is running on the debug port you have selected.
These Settings Work
These settings are what I am using now and work well.
Windows
- You need to set a static IP on your ethernet adapter (or wifi)
php.ini
- Use your static IP to set xdebug.remote_host. Setting a static IP seems to fix most connecion issues. xdebug does not like 127.0.0.1 it works sometimes but can be problematic.
- Swapping xdebug.remote_port to 9001 helped when xdebug just stopped working. The default is 9000.
- Disable output_buffering in your php.ini by altering/uncommenting the following line
output_buffering = Off
- Add the following code to the end of your php.ini file.
[XDebug] zend_extension="D:\websites\php\ext\php_xdebug.dll" ;zend_extension="D:\websites\php\ext\php_xdebug-2.5.4-5.6-vc11.dll" xdebug.idekey = netbeans-xdebug xdebug.profiler_append = 0 xdebug.profiler_enable = 0 xdebug.profiler_enable_trigger = 0 xdebug.profiler_output_dir = "d:\websites\tmp\xdebug" xdebug.profiler_output_name = "cachegrind.out.%t-%s" xdebug.remote_enable = 1 xdebug.remote_autostart = 0 xdebug.remote_connect_back = 0 xdebug.remote_host = "192.168.1.160" xdebug.remote_port = 9001 xdebug.remote_handler = "dbgp" xdebug.remote_mode = req xdebug.remote_log = "d:\websites\tmp\xdebug\xdebug_remote.log" xdebug.show_local_vars = 9 xdebug.trace_output_dir = "d:\websites\tmp" ;xdebug.show_exception_trace = 1
Minimum working settings
These are the minimum settings I have found to work but are here only for reference.
[XDebug] ;zend_extension="D:\websites\php\ext\php_xdebug.dll" zend_extension="D:\websites\php\ext\php_xdebug-2.5.4-5.6-vc11.dll" xdebug.remote_enable=1 xdebug.remote_handler=dbgp xdebug.remote_mode=req xdebug.remote_host=192.168.1.160 xdebug.remote_port=9001 xdebug.idekey=netbeans-xdebug
Netbeans
(Tools --> Options --> PHP --> Debugging)
- Debugger Port: 9001
- Session ID: netbeans-xdebug
- Maximum Data Length: 2048 - I think this is the default
- Stop at First Line: off
- Watches and Balloon Evaluation: All off
- Show Requested URLs: off
- Show Debugger Console: On
Browser
You need to send a trigger to Xdebug via your browser to trigger the debugger. You can send the trgger via GET/POST but you need a plugin for this.
- Firefox
- The easiest Xdebug – Add-ons for Firefox
- There are many others....
- Google
- Xdebug helper - Chrome Web Store
- There are many others....
Test Debugger is working
Now you have configured your system as above you need to test it before you can rely on it.
- Set a breakpoint in Netbeans on the index.php (or a PHP file that is parsed) in your software project, preferably one before the content is displayed. (or you can enable the ‘Stop at First Line’ option in the Netbeans config
- Start debugging in Netbeans
- Open your project in a chrome browser with the xdebug helper plugin enabled
- If the browser and Netbeans stop at the breakpoint, everything is working
Diagnostics
Things that Might help
If xdebug does not work straight away then trying these things might help.
- could disabling ipv6 in the network adapter help? because xampp has issues with ipv6
- xdebug might fail if you have multiple version of xampp running
- Localhost is mapping to ::1 and not 127.0.0.1
- Not all lines can act as breakpoints, these are displayed as cracked breakpoint markers in Netbeans. These breakpoints will not work because Netbeans does not like them. To fix this yoou should choose another breakpoint.
- Run the Xdebug on another server which has another IP
- Get the latest dll from xdebug site (i.e. php_xdebug-2.5.4-5.6-vc11.dll)
- Comment out any zend extensions
- Some people use port 9001 when they have issues with 9000.
Test the port is not already in use - dbgtest.php
This code is taken from 21.4.2 How to Set Up XDebug | Oracle
<?php $address = '127.0.0.1'; $port = '9000'; $sock = socket_create(AF_INET, SOCK_STREAM, 0); socket_bind($sock, $address, $port) or die(); socket_listen($sock); $client = socket_accept($sock); echo "Connection established: $client"; socket_close($client); socket_close($sock); ?>
However I found this slightly better version from dbgtest.php · GitHub
<?php // adapted from https://blogs.oracle.com/netbeansphp/entry/howto_check_xdebug_installation $address = $argv[1] ?: '127.0.0.1'; $port = '9000'; $sock = socket_create(AF_INET, SOCK_STREAM, 0); socket_bind($sock, $address, $port) or die(); echo "Listening to $address:$port\n"; socket_listen($sock); $client = socket_accept($sock); echo "Connection established: $client\n"; socket_close($client); socket_close($sock);
- Create a php file with the code above called dbgtest.php and put it in the htdocs folder of your xampp server
- Change the port to 9001 if that is the port number you are using/diagnosing
- Enable sockets - extension=php_sockets.dll
- only be need for the test script
- You enable this by uncommenting the appropriate line in the php.ini
- if you do not enable this you will get this error
Call to undefined function socket_create()
- Close Netbeans if that is running.
- Restart Xampp
- Run the script
If there is something running on this socket you will get an error as shown below. This shows that some other process is using the port 9000.
Warning: socket_bind(): unable to bind address [10048]: Only one usage of each socket address (protocol/network address/port) is normally permitted. in D:\websites\htdocs\dbgtest.php on line 5
Workarounds
These are a few workarounds I tried and might work for you. These are not designed to be a permanent fix.
Method 1
- Make sure netbeans and xampp are not running
- Put the basic xdebug settings in php.ini
- Enable extension=php_sockets.dll in php.ini
- Run xampp
- Put a copy of the oracle script in your Webroot and run it. This causes xampp to open socket on 9000
- Now open and run NetBeans debugging
* I am not sure if you need to do the IP fix aswell for this to work. It is all caused by routing issues on the loopback system of the PC. Or sockets might be needed.
Method 2
- Turn of netbeans debugging (leave netbeans open)
- Run the dbgtest.php to allow xampp/xdebug to grab the port
- Now start netbeans debugging
Method 3 - (if working and then stops for no reason)
- Close Netbeans, Xampp and browsers
- Restart them all