Toggle XDebug Bookmarklet

If you code PHP, you probably use XDebug for debugging your code. Some IDEs deal with the gubbins required for starting and stopping the debugger for you, but other debugging clients (like DBGp Client for Vim) require you to manually append XDEBUG_SESSION_START=1 to the end of your page’s query string before you try to connect the debugger.

Inspired by this blog post, I wrote a bookmarklet to toggle XDebug on or off at the click of a button, by adding XDEBUG_SESSION_START=1 if it’s not already in the URL, or toggling between XDEBUG_SESSION_START=1 and XDEBUG_SESSION_STOP=1 if it is.

To add it to your browser, drag the following link to your Bookmarks toolbar
Toggle XDebug

The un-minified code is shown below:

javascript: (function() {
    var href=window.location.href;
    if (href.indexOf("XDEBUG_SESSION_START") != -1) {
        window.location.href = href.replace("START", "STOP")
    } else if (href.indexOf("XDEBUG_SESSION_STOP") != -1) {
        window.location.href = href.replace("STOP", 'START")
    } else {
        if (href.indexOf("?") != -1) {
           window.location.href = href+"&XDEBUG_SESSION_START=1"
        } else {
         window.location.href = href+"?XDEBUG_SESSION_START=1"
        }
    }
})()