Simple cross-browser Xdebug helper. Session starter and stopper, no add-ons needed.

During a recent browser upgrade I found myself stuck in a bit of a corner. The Firefox add-on I had been using, Xdebug Helper, was discontinued, and the supposed replacement add-on for it didn't work correctly.

Since the functionality of this now-defunct add-on made my life a lot easier (e.g., don't have to manually append/strip '?XDEBUG_SESSION_START=default' in my browser all day long to start/stop debugging sessions) I took it upon myself to keep this functionality and perhaps get rid of yet-one-more-add-on (which pays off when upgrade time comes).

In all their simplicity, here are two bookmarklets you can use to start and stop a Xdebug session in your browser of choice. Note that if you are using a custom proxy key value then you'll need to change the '=default' part in the bookmarklet to '=YourProxyKey'.

The 'start session' bookmark
Save this bookmark and then all you need to do to start a debugging session (assuming that you have Xdebug setup correctly and a breakpoint set in your code, of course) is to select the bookmark. I chose to put my bookmarks in the Toolbar at the top of window to make it even easier to get to. Also, just for the sake of posterity here is the code for the bookmark.
javascript:(function(){location.href=location.href+'?XDEBUG_SESSION_START=default';})();

The 'stop session' bookmark
Save this bookmark and put it next to your 'start session' bookmark. When you're done with your session, simply click this bookmark and viola. Again, here is the code:
javascript:(function(){var currentUrl=location.href;var gotoUrl=currentUrl.replace("?XDEBUG_SESSION_START=default","");document.cookie='XDEBUG_SESSION=default;expires=Fri, 3 Aug 2001 20:47:11 UTC;host=none;path=/';location.href=gotoUrl;})();

1 February, 2010

Comments

Cool.

Just looking from the code though it doesn't look like it will work with AJAX. Since you're just manipulating the Location object (i.e. the current page). So if you want to do something like debug an ajax callback you'll be out of luck.

I wonder if there's any way to do that.

Actually, I was working with AJAX calls today and it was working fine, with a little caveat - start debugging for the entire page in general just to set the session. Once you've done that, any AJAX calls from the page will trigger the debugger since the session is set.

Awesome! But does it work if the URL already has a query string? Or is the query string overwritten/lost?

I think the thing to do with query string would be the same thing to do for AJAX calls - basically just trigger debugging for the page in general (e.g., without taking query strings into account), and then after that the xdebug session will be set, which should then let you trigger the debugger even with query strings in the url. Hope that makes sense/helps.

Heh, I also ran into this problem a couple of days ago and ended up with another add-on that basically does what your bookmarks do, but I think I like the bookmark idea more. One thing I liked about the xdebug helper is that it appended the variable to your next request, rather than starting a new request by refreshing the page, so maybe a little javascript to append the session start to all of the paths via javascript. I might give that a shot, now that I see that you can do javascript in a bookmark. Very cool, thanks man!

Thanks!
Chris

Sure thing, Chris. If you get something like you're talking about working, please share! :-)

Thanks a lot for this interesting information. I had exactly the same problem, and after trying your bookmarks, everything works as it should. Your solution is really easy and practical, so your article is really helpful. Thanks for sharing our knowledge!

I might give it a shot, but I still like the idea of not having one more extension to worry about at upgrade time, plus the bookmarklets are cross-browser. See you at Drupalcon soon I imagine, Chris. :-)

Good idea but it doesn't work on an HTML file which calls a PHP file via JavaScript. But then I suppose that's asking a bit too much ;)

Thanks for the tip though. Looks good for standard PHP files in a URL.

Hi,
the proper syntax to handle urls with or without query strings is :

javascript:location.href=location.protocol+'//'
+location.hostname+location.pathname+(location.search%20?%20location.search%20+'&'%20:%20'?')
+'XDEBUG_SESSION_START=default'

Thanks. Best documentation for this on the web.

Really, you made my life a whole lot better by doing this.