Skip to main content

Debugging php application with xdebug, netbeans and Google Chrome

Debugging PHP application is a pain. When we get problems, we often to use var_dump() or die() in various location to inspect the variables and then clean them again once the problem is solved. Fortunately, there is a piece of software called xdebug which can tremendously help us debugging php code. This article is about using xdebug in linux environment.




  1. What we need:
    • A working apache2 web server with PHP
    • Netbeans: an excellent IDE for writing PHP application
    • xdebug
    I assume you already have the first two.
  2. Install xdebug, open linux terminal, change to root and install xdebug with command (pecl install debug). See below
    bambang@bambang-notebook:~$ sudo su
    [sudo] password for bambang: 
    root@bambang-notebook:/home/bambang#pecl install xdebug
    downloading xdebug-2.1.2.tar ...
    Starting to download xdebug-2.1.2.tar (Unknown size)
    .................................done: 1,340,416 bytes
    66 source files, building
    running: phpize
    Configuring for:
    PHP Api Version:         20090626
    Zend Module Api No:      20090626
    Zend Extension Api No:   220090626
    .....
    ..... 
    ......
    Build process completed successfully
    Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so'
    install ok: channel://pecl.php.net/xdebug-2.1.2
    configuration option "php_ini" is not set to php.ini location
    You should add "extension=xdebug.so" to php.ini
    
    
    Note where the xdebug.so is installed in the line Installing '/usr/local/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so'
  3. Add these lines to your php.ini. In my case /usr/local/lib/php.ini. 
  4. :
    zend_extension="/usr/local/lib/php/extensions/no-debug-non-zts-20090626/xdebug.so"
    xdebug.remote_enable=on
    xdebug.remote_log="/var/log/xdebug.log"
    xdebug.remote_host=localhost
    xdebug.remote_handler=dbgp
    xdebug.remote_port=9000
    
  5. Restart apache
    root@bambang-notebook:/home/bambang# /etc/init.d/apache2 restart
     * Restarting web server apache2                                                 ... waiting                                                             [ OK ]
    root@bambang-notebook:/home/bambang# 
    
    
  6. Write a phpinfo.php page in your web directory like this:
    <html>
    <head>
    </head>
    <body>
    <?php echo phpinfo(); ?>
    </body>
    </html>
    
  7. Open the phpinfo.php in your browser and check whether the xdebug is successfully installed.
  8. Now go to Netbeans and navigate through menu Tools->Option->PHP and make sure the Debugger port is the same with what we set in the php.ini above. I clear Stop at First Line check mark, but it is okay if you let it checked. If it is checked, the debugger will stop at first php statement. But I only want it stops on break point.
  9. Open your chrome,  goto https://chrome.google.com/webstore/detail/eadndfjplgieldjbigjakmdgkmoaaaoc and install the xdebug helper.
  10. Now we are ready to debug, in this example I'll debug my CodeIgniter application. First I open one of my controller: daftar_negara.php and place a breakpoint in line 11 by clicking the line number or press ctrl-f8.
  11. Still inside Netbeans run the debugger by clicking menu Debug->Debug Project or clicking the debug icon in the netbeans taskbar like (see the image below):
    The debugger will launch our project in chrome. Now we need to activate chrome xdebug helper, click the xhelper debug icon in right end of the address bar until it turns to green.
  12. Now press F5 (refresh), xdebug will stop the execution on our breakpoint and switch to our neatbeans:
    Now, we can step over, step info etc. via netbeans debugger tool bar, of course you can also use the shortcut key (F8,F7 etc):
    We can also inspect the variable values in the window below the code window:
From here, I am sure you can figure the rest yourself. Happy bug hunting!

Comments

  1. I use and recommend CodeLobster IDE for it - http://www.codelobster.com

    ReplyDelete

Post a Comment

Popular posts from this blog

Install Sketchup 2017 64 bits on Linux Ubuntu 16.04 64 bits

Install Sketchup 2017 64 bits on Linux Ubuntu 16.04 64 bits: 1.Enable 32 bit architecture: $sudo dpkg --add-architecture i386  2. Set wine PPA $sudo add-apt-repository ppa:wine/wine-builds Update repository $sudo apt-get update 3. Install wine newest staging branch version $sudo apt-get install --install-recommends winehq-staging 4. Ensure we get a 64bits wine, edit file ~/.profile and locate for text: export WINEARCH= If the value is win64 you are good, if the value is win32 change it to win64. Save the file. Ensure the environment variable also set to win64 by typing command: $export WINEARCH=win64 5. Download winetricks: $wget https://raw.githubusercontent.com/Winetricks/winetricks/master/src/winetricks    Don't forget to set the attribute so it is executable $chmod +x winetricks 6. Run winetricks $./winetricks Inside winetricks: Choose Select the default wineprefix, click OK Choose Install a Windows DLL or component, click OK Choos

Program timbangan elektronik, sebuah program Windows yang dijalankan di Linux.

Kemarin, mumpung hari libur aku setup program timbangan digital baru di kantor. Program yang lama merupakan program DOS yang aku tulis dengan menggunakan Clipper, program ini berjalan dengan baik di linux dengan bantuan dosemu. Program baru merupakan program Windows. Salah satu improvement dalam program ini adalah support untuk beberapa jenis indikator. Program diinstall di linux dengan wine versi 1.1.20. Beberapa catatan dalam instalasi program ini di wine: Pencetakan slip bukti timbangan dan laporan-2 menggunakan printer Dot Matrix. Apabila pencetakan dilakukan lewat printer driver, pencetakan dengan printer dot matrix akan menjadi sangat lambat plus hasil buruk plus boros pita... :) Oleh karena itu, program ini melakukan pencetakan langsung melalui printer port lpt1. Di windows, hal ini tidak menjadi suatu permasalahan, di linux dengan wine, maka lpt1 akan dimap ke /dev/lp0. Masalahnya, owner dari device ini adalah root dan group lp. Secara default user-user di Ubuntu (..nggak tahu

Disabling middle click paste and close in ubuntu 18.04 by enabling /etc/rc.local

How to disable middle click button in ubuntu 18.04 by enabling /etc/rc.local Create or edit/etc/rc.local file #!/bin/bash xinput set-button-map 11 1 0 3 exit 0 Make sure it is executable $sudo chmod 755 /etc/rc.local Edit rc-local.service file sudo vim /etc/systemd/system/rc-local.service   Add this section [Install]  WantedBy=multi-user.target Enable the service sudo systemctl enable rc-local