Home > Tags > User (computing)
Page 1

PHP Error Checking

“Just when you think you’ve made something foolproof, God makes a better brand of fool.”

This maxim of manufacturing also applies to software development, especially on a highly accessible technology like web applications. As much as programmers attempt to anticipate every possible action or combination of actions that a user can take when encountering a web application, no one can foresee them all. When the user takes an unanticipated course of action and “breaks” the application, the software needs to catch them before they fall.

Die() Function

One of the techniques that early PHP programmers used to catch errors was the “die” function. When the program would “die”, this function stops the script and displays an error message.

<?php if(!file_exists("hello_world.txt")){ die("File not found"); } else { $file=fopen("hello_world.txt","r"); echo(“Hello World file found”); } ?>

Without the die function, the program would return a specific error message, but not one that users could readily understand:

Warning: fopen(hello_world.txt) [function.fopen]: failed to open stream:No such file or directory in C:\wwwroot\developer_drive\file_opener.php on line 2

Exception Handling

As with other C-based languages (C#, Java, etc.), PHP 5 has a technique for catching exceptions. The keywords “try”, “throw” and “catch” find exceptions and enable better error handling.

Try: Any function that employs the use of an exception to handle errors should be in a “try” block. If the exception is not triggered, the rest of the script will continue executing as written. If the exception is triggered, the exception is said to be “thrown”. Throw: The “throw” block triggers an exception. For each “throw” block, there must be one or more “catch” blocks. Catch: The “catch” block retrieves an exception and instantiates an object that holds the exception data.

<?php try { $error = 'Throw this error'; throw new Exception($error); // Code following an exception is not executed. echo 'This line is ignored'; } catch (Exception $e) { echo 'Exception caught: ', $e->getMessage(), "\n"; } // Continue execution echo 'This line is executed.'; ?>

The Exception class that the catch block creates contains several methods: getMessage()- displays message of exception getFile() – finds error source filename getLine() – finds error source line getCode() – displays code of exception getTrace() – creates an array of the backtrace() getTraceAsString() – formats string of trace array

Trigger Error

In most cases, programmers attempt to avoid errors. In some instances, however, developers may want to test their error handling capabilities.  The “trigger_error” function throws an error message when the program encounters an error:

<?php $text= “Hello”; if ($text !== “Goodbye”){ trigger_error(“Say Goodbye. Error Triggered.”); } ?>

Error Messages

Not only do applications have to catch errors before the user encounters them, they must present the reason for the error in a way that the user will understand why it “broke”.

Error messages should always be easy to read for the users; these message allows the user to remain confident that the application can be fixed.  The error messages also help developers chase down potential problems.

Remember, confused users can become angry users, and a user’s anger can lead to a developer’s impending unemployment.

...
more →
Alex Vernacchia says: You would use try/catch when you throw an exception as an error and you want to don't want to break your application. For...

PHP User Survey Part V: Administration Layer

In the last piece on our PHP online user poll, we look at the administrative service and how the site supervisor enters, deletes and manages the poll data.

The first poll administrative page checks if the administrator is logged in. You can choose from Session variables or Cookies to check the site administrator login status.

Once the application has confirmed the identity of the site administrator, the page lists the available polls.

The first step is to use the class methods and variables in the class.polls.php file:

<?php require_once("class.polls.php");

The next steps include instantiating the poll object and retrieving the poll data:

// instantiate polls class $oPolls = new polls; // get polls and poll count $aPolls = $oPolls->getPolls("created_dt desc", $iCursor); $iCnt = $oPolls->getPollsCount();

We create an array to hold and display the data from the polls table in the database:

// check for polls if (count($aPolls)) { // build page data array $i = 0; while ($i < count($aPolls)) { $aData[$i]["Id"] = $aPolls[$i]["Poll Id"]; $aData[$i]["Name"] = $aPolls[$i]["Question"]; $aData[$i]["Status"] = $aPolls[$i]["Status"]; $aData[$i]["Created"] =$aPolls[$i]["Created Date"]; ++$i; } }

For a given poll ID number, we can delete, activate or deactivate a selected poll:

// check for id if ($id) { // assign poll id $oPolls->setPollId($id); // check operation type if (!strcmp($op, "del")) { // try delete poll and redirect $oPolls->deletePoll(); header("Location: ".SELF); } elseif (!strcmp($op, "act")) { // try activate poll and redirect $oPolls->activatePoll();...
more →
Logo Design Contest says: Good to read about administration concept which makes an incredible changes in the environment of reputable firms.