How to set up server side PHP debugging with Zend and Eclipse

May 1st, 2009 in PHP. 4 comments
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading ... Loading ...

Almost all php scripts are used in web apps and therefore it is essential to debug scripts in the context of a web application rather than debugging a php file in isolation. This recipe describes how to set up the Zend debugger for debugging php scripts in a running web application.
The example used here uses WAMP. However the configuration will mostly be similar in any PHP/Apache/eclipse environment.

Steps

  • Download the All-in-One Eclipse PDT + Zend Debugger Package from the eclipse site.
  • Find and note the location of the ZendDebugger.dll (ZendDebugger.so for linux). It can usually be found in the eclipse/plugins/org.zend.debug.debugger.<version>/resources/php5.
  • Edit the php.ini file (in your apache/bin), adding the following towards the end of the file:
  • zend_extension_ts=/your/path/to/ZendDebugger.dll
    zend_debugger.allow_hosts=127.0.0.1
    zend_debugger.expose_remotely=always

    You may add more hosts by seperating the ip list with commas.

  • Start your WAMP server and navigate to http://localhost/phpinfo.php on your browser and click on the link phpinfo(). In this page you should see an entry for the Zend debugger which looks something similar to this:
    zendsettings How to set up server side PHP debugging with Zend and Eclipse
    If you do not see the above table then php is not able to find your dll file.
  • Your eclipse download should have have a dummy.php file. If it doesn’t or you cant find it ( I know I couldn’t), create one containing the following code and place the file in your Document Root.

    <?php
    @ini_set('zend_monitor.enable', 0);
    if(@function_exists('output_cache_disable')) {
        @output_cache_disable();
    }
    if(isset($_GET['debugger_connect']) && $_GET['debugger_connect'] == 1) {
        if(function_exists('debugger_connect'))  {
            debugger_connect();
            exit();
        } else {
            echo "No connector is installed.";
        }
    }
    ?>
    
  • In eclipse, create/import your php project with the php files you intend to debug. For our purposes here let us assume that our project has the following directory structure. We also assume here that: a) myphpproject is our document root, b) requests are sent from index.html to phpfile1.php and c) we want to debug phpfile1.php
  • myphpproject
    |__index.html
    |__lib
       |___phpfile1.php
       |___phpfile2.php

    The next sections assume the above, if your directory structure is different then change the values accordingly.

  • Right click on your project then select: Debug As->Debug Configurations.
  • On the left pane of the Debug Configuration window, select PHP Web Page, right click and create a new configuration. In the resulting pane on the right hand side select the Server tab, then select the Server Debugger as Zend Debugger and click on the Test Debugger button. If the debugger is configured correctly you should get a success message.
  • Now click on the Configure button. In the Server tab of the resulting window, fill in the values according to your configuration. In our case, since we are running WAMP on the localhost we enter http://localhost as URL. In the Path Mapping tab, click on Add and enter the path on server and path in workspace (http://localhost and /myphpproject respectively, in our case here) and click on OK:
    zendserverconfig1 How to set up server side PHP debugging with Zend and Eclipse
  • Now back at the Debug Configuration window, enter the values for the File you want to debug and the URL. In our case here we assume that we want to debug phpfile1.php and requests are sent from index.html (either through a form or otherwise) to this php file where we want to start debugging. Therefore this is what our values look like:
    zendserverconfig2 How to set up server side PHP debugging with Zend and Eclipse
  • Click on the Advanced tab and select where you want to start debugging. We select the Start Debug From option and enter http://localhost/lib/phpfile1.php. Also check the Open in browser option.
  • Now click on the Apply button and then the Debug button. Your web application should start in a browser window in eclipse. Carry out whatever action you need to do on the web page to send the request to your php file. Eclipse will open in debug perspective and execution will suspend on the first line of your php file. From here debug as usual.

Troubleshooting

  • Test Debugger Fails Check that you have the correct IP/host in Window->Preferences->PHP->Debug->Installed Debuggers->Zend Debugger->Configure
  • Connection to Apache resets on consecutive debugs: After each debug session is complete, click on the terminate button (the red button seen in debug perspective) in Eclipse and then start a new session.

    Tags: , ,

4 Responses to “How to set up server side PHP debugging with Zend and Eclipse”

  1. Peter at
    says:

    “If you do not see the above table then php is not able to find your dll file.”

    Any idea why this might happen? Initially, I was editing the wrong php.ini… I’ve verified that the changes I make to php.ini are now noticed.

    I’ve tried some basic things: all sorts of combinations of normal / back slashes, adding a junction so that the file was on the same volume (in case it didn’t understand “c:\…” for some reason), added “file://” to the path… I don’t really see what the problem might be.

  2. admin at
    says:

    Not sure what your problem could be. I currently have mine at C:\Program Files\eclipse\plugins\ZendDebugger.dll on my laptop and it is able to find it.

  3. mischa at
    says:

    For PHP5.3 or higher use the following in your php.ini file

    zend_extension=/your/path/to/ZendDebugger.dll

  4. Peter at
    says:

    Thanks for the comments. I gave up for a while, and tried again tonight. mischa was right that I should have had zend_extension with no _ts… but unfortunately that still made no difference (think I might actually have tried that before).

    I now seem to have things almost working with xdebug ala http://programmingbulls.com/debugging-drupal-php–xdebug-eclipse... when I finally manage to stop on a breakpoint and see a stacktrace then I’ll be happier. Or not, because I’ll have wasted all my precious time setting up tools when I could probably have got by with the odd echo statement…

Leave a Reply

Sponsors