VSource Web Solutions About Us  |  News  |  Contact Info  |  Network  |  Policies  |  Login  |  Support  |  Forum  |  (866) 346-9327


Online Demo



   
 
Search Documentation Search Documentation
 
   

Debugging Your VConsole Applications

   
 
Table Of Contents - Collapse

NEXT: VConsole Adanced Techniques
 
   

Introduction
One of the most important thing to know when developing your VConsole applications is how to debug your code. The great news is that VConsole makes debugging your applications quick and easy. In this section of this publication, we go over how to spot errors in your code, how to figure out where an error is, and even how to debug AJAX communication between the server and web browser. VConsole even provides plenty of tools that allow you to debug your software applications during the development stages and even after your product has been deployed in a production environment. Let's get started.

Setting Up Your Debugging Environment
The first thing that you should do in order to start debugging your applications is to setup the correct debugging environment. This includes setting up your server environment as well as your web browser debugging environment.

  • Set PHP to display error messages. This involves modifying the php.ini file and ensures that your server outputs error messages that are related to code errors. You don't want to show all messages because you may get overwhelmed with tons of warnings that are not necessarily fatal to your program. If you are developing on a hosted environment, then you will most likely not have access to the php.ini file so you can disregard this step. Most likely, your web host has already setup the PHP environment to display only fatal error messages. For those of you that are developing on a dedicated server where you have access to the php.ini file, please set the following parameters in your php.ini file. You can simply do a search for these parameters as they will already be present in the file.
    • error_reporting = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR
    • display_errors = On
       
    Once you have made these changes in the php.ini file, you will want to restart your web server. Usually you can do this by typing the command: service httpd restart at a shell prompt.

    UPDATE: As of Version 4.5-9, you should not need to setup error_reporting because it is setup at runtime within the software program.
     
  • Install Firebug in your FireFox browser. Firebug allows you to see all the activity of what is being sent to the server and what is being received from the server. You can debug your javascript (if you have any custom javascript) and all AJAX communication to and from the server. You can also see what POST and GET variables are sent to the server for any given AJAX operation. Remember that the entire VConsole interface is written in AJAX. This means that the interface never sends a standard browser request and the page is never redrawn by the browser. So the only way that you can see what is happening is to install the Firebug add-on. To install Firebug, open up your FireFox browser and go to http://www.firefox.com. Next, click on "All Add-ons" and do a search for "Firebug". Then click on the button to add Firebug to your browser. Once Firebug is installed, you will see a small bug looking icon in the lower right corner of your browser. Click on that icon to see what is going on as you click on items within VConsole. Using Firebug is beyond the scope of this publication, however, we do recommend learning how to use it to debug your applications. You can read more about Firebug and how to use it at http://getfirebug.com

Debugging Your PHP Code
There are likely 2 types of errors that you will encounter while coding your PHP files. The first error that you will likely encounter is PHP syntax errors. When these happen, your PHP file does not run and instead you get a big fat error message telling you why the file won't run (assuming you have setup your php.ini file to display error messages). The second likely error is a bit more elusive because these types of errors will happen when you don't get the expected results when you execute your PHP files. You'll find that the PHP file executes without complaining, but after everything is finished executing, something didn't happen as it was supposed to. These types of errors are a bit harder to debug but we will go over some techniques that allow you do figure out exactly what is happening.

  • Debugging PHP Syntax Errors. Finding and correcting PHP syntax errors is rather easy. This is because the PHP error that is output to your browser tells you what line of what file the syntax error is on. Most of the time, this will be correct so all you need to do is find the error on that line (or on the previous line) and correct it. A common mistake of PHP programmers is to not put the trailing semi-colon at the end of a PHP statement. This usually causes a PHP syntax error on the next coded line of the PHP file.  Another common mistake is to forget a closing curly bracket somewhere in your file. These errors are harder to find because the PHP error will usually tell you that the error is on the last line of the PHP file even though it is actually not. To troubleshoot these types of syntax errors, you may have to eliminate code blocks in your code until the PHP file doesn't give any errors. Then you can usually figure out where in your file you forgot to place the closing curly bracket.
     
  • Debugging PHP Files That Give Unexpected Results. These types of errors are called functional errors and are harder to debug than syntax errors. This is because your PHP file executes as it should without complaining or telling you where the error is. All you know is that what is being displayed is not what you expected, or maybe data isn't getting stored in the database as it should. In this case, you have to debug your code section by section or even line by line. You can use the debug() and html_print_r() functions to debug your code in these situations. This technique involves placing one of these functions somewhere in your code where you think that the error may be occurring. Knowing where to place these functions and what to pass to them is a valuable asset that can only come with experience when it comes to debugging your PHP code. This is not something that we can teach because everybody's PHP code is different. The general idea is to put an html_print_r() statement somewhere in your script and check out the output. If the output is what is expected, then move the statement a bit further down in your code and check the output there. If the output is not what is expected, then the error already occurred and you must move the statement up in your code. Remember, the html_print_r() function simply prints out the structure of a variable, an array, or an object, so pass the variable, array, or object that you want to see the contents of to the html_print_r() function as an argument. Keep doing this until you narrow down the error to a single line or statement. I just want to stress one more time that these types of bugs are called functional errors and are the most difficult to debug because they output no errors. (For more information on the html_print_r() and debug() functions, please see PHP functions).
     
  • Getting the Output From An AJAX Request. The 2 debugging techniques above tell you what to look for and how to output data to the browser, however, because the entire VConsole interface is programmed in AJAX, the output may not show in the browser as expected. If this is the case, then you need to get the output from the AJAX response. You can do this using Firebug. We mentioned above that you should install the Firebug add-on for your FireFox web browser before you start debugging. Once you have done this, all you need to do is expand the Firebug tools by clicking on the small bug icon in the lower right corner of your browser. Next click on the "Console" tab and find the AJAX request that you want the output from. Finally, click on the "Response" tab and click on "Load Response". This will tell you exactly what was output by the server as the AJAX response.

http://getfirebug.com/
If you have loaded your own custom javascript files into VConsole, and you need to debug them, you should simply use Firebug. When you have a javascript error, it will tell you about it, let you know where the error is, and attempt to tell you how to fix it. Firebug is truly an indispensable tool when it comes to debugging your VConsole applications.


NEXT: VConsole Adanced Techniques


COMMENTS
 

There are no comments at this time.

Post Your Comment
 
Your Name*
Your Email Address* (Not Shown)
Your Comment (HTML OK)*
Enter the characters that you see here
Get New Image
*required fields