Debugging Detective Work

I l @ ve RuBoard

When it comes to the more challenging type of errorsprogrammer errors, PHP won't help you out by telling you on what line to begin looking. In fact, all that you'll probably see is that the results weren't what you were expecting. In order to solve these riddles, you'll need to do a little detective work to see what mistakes were made and where.

There are three tools and techniques to help you in your cause:

  • Using comments.

  • Using the print() statement.

  • Tracking variables .

Just as you can use comments to document your scripts, you can also use them to rule out problematic lines. If PHP is giving you an error on line 12, then commenting out that line should get rid of the error. If not, then you know the error is elsewhere.

In more complicated scripts, I frequently use the print() statement to leave myself notes as to what is happening as the script is executed. When a script has several steps, it may not be easy to tell if the problem is occurring in step two or step five. By using the print() statement you can narrow the problem down to the specific step.

Finally, it is pretty easy for a script not to work because you referred to the wrong variable or the right variable by the wrong name . To check for these possibilities, use the print() statement to let you know what the values are of the variables as the script progresses. Then you will know for sure if the variables are causing a problem.

I'll illustrate these techniques by modifying the HandleLogin.php page from earlier in the chapter to demonstrate how I would debug it if I were getting unpredictable results.

To debug a script:

  1. Open HandleLogin.php in your text editor (Script 14.2).

  2. Delete the initial blank line created earlier making the initial PHP tag the first line of the page (Script 14.5).

     <?php 
    Script 14.5. If the HandleLogin.php page was acting strangely and giving you results that you couldn't understand, you could modify it like this for debugging purposes. The combination of using print() statements, tracking variables, and commenting out lines of code will quickly help to solve any problems.

    graphics/14sc05.jpg

  3. After the headers_sent() conditional (line 2), add a print() statement:

     if ( headers_sent() ) {      print ("Headers have been sent. Not       attempting to verify.<P>\n");       print ("Cannot process your request        due to a system error!\n"); 

    If the problem is because the headers have already been sent, you'll make that clear to yourself while testing.

  4. After the else (line 5), add three more print() statements.

     } else {     print ("Headers have not been sent.      Attempting to verify.<P>\n");     print ("UserName is $UserName.      <P>");     print ("Password is $Password.      <P>"); 

    The first print() statement correlates to the one added above, indicating that the problem is not header() related .

    The next two print statements will tell you exactly what values were received for $UserName and $Password. You can track the value of variables throughout a script by printing them out at key moments.

  5. After the second if conditional (line 9), add another print() statement and comment out the header() line.

     if (($UserName == "Larry") &&      ($Password == "LarryPass")) {         print ("<P>Match!");         header ("Location: index.php?          UserName=$UserName");         exit;    } else { 

    By printing the word Match! you'll know whether or not the system verified the username and password, which may let you rule that out as a possible cause of error.

    Since you are debugging the script, you can eliminate the effect of the header() call by placing two slashes at the beginning of the line to comment it out. Simply removing the backslashes when you are ready uncomments the line and reinstates the command.

  6. Again, add another print() statement and comment out the header() function after the else (line 13).

     print ("<P>Not a Match!");          header ("Location: login.php?           Message=Invalid");          exit;     } } ?> 
  7. Save your script as HandleLogin.php, upload it to the server in the same directory as login.php, and test both pages in your Web browser (Figures 14.8,14.9, and 14.10).

    Figure 14.8. In debugging a problem script, printing messages detailing the process that PHP is going through can be invaluable. Here you can determine that the problem is not caused by the headers or referring to the wrong variables.

    graphics/14fig08.gif

    Figure 14.9. Again, the specifics of what HandleLogin.php is doing is spelled out for you. If this page is causing any problems, you can now narrow it down to the header() lines.

    graphics/14fig09.gif

    Figure 14.10. This is all too-common of an occurrence. The reason that no match is being made is because the wrong variable name is being referred to. Notice that there is no value for UserName. This means there is a discrepancy between what login.php and HandleLogin.php use to refer to this value.

    graphics/14fig10.gif

I l @ ve RuBoard


PHP for the World Wide Web (Visual QuickStart Guide)
PHP for the World Wide Web (Visual QuickStart Guide)
ISBN: 0201727870
EAN: 2147483647
Year: 2001
Pages: 116
Authors: Larry Ullman

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net