Home arrow PHP arrow Page 2 - User Authentication With patUser (part 3)

Making Exceptions - PHP

In this concluding article, find out about how to use patUser toidentify users and groups by different criteria, track a user's clicks, maintain user statistics, and gracefully handle errors.

TABLE OF CONTENTS:
  1. User Authentication With patUser (part 3)
  2. Making Exceptions
  3. The History Channel
  4. Natural Selection
  5. No Distinguishing Marks
  6. Big Brother Is Watching
  7. Endgame
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 6
May 07, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
First up, error handling. You'll have seen, from the previous sections of this article, that many of patUser's functions return either true or false to indicate success or failure; this value can then be used in your script to display an appropriate status message.

However, patUser also goes further, allowing you to display a more verbose message indicating the cause of error if one occurred. A built-in error stack keeps track of all the errors that have occurred, and the library include a number of functions designed to help you read and modify this stack.

The two functions you're most likely to use are getLastErrorCode() and getLastErrorMessage(), which return the last error code and message respectively. Consider the following example, which demonstrates:


<?php // include classes include("../include/patDbc.php"); include("../include/patUser.php"); // initialize database layer $db = new patMySqlDbc("localhost", "db211", "us111", "secret"); // initialize patUser $u = new patUser(true); // connect patUser to database $u->setAuthDbc($db); // set tables $u->setAuthTable("users"); // add user $u->addUser( array("username" => "tom", "passwd" => "tom") ); // add user again $u->addUser( array("username" => "tom", "passwd" => "tom") ); // get message and display echo "Operation failed. " . $u->getLastErrorMessage() . " (#" . $u->getLastErrorCode() . ")"; ?>
Since there's a subtle error in the script above, patUser will not be able to execute the call to addUser() successfully. An error code will be generated internally and added to the error stack; this code can be viewed and retrieved via the calls to getLastErrorCode() and getLastErrorMessage(). Here's an example of what you might see:



Obviously, this is a little more informative than the standard "User could not be added" message.

You can also use the shortcut getLastError() method, which returns an array containing both error code and message. The following example, which is equivalent to the one above, illustrates:

<?php // include classes include("../include/patDbc.php"); include("../include/patUser.php"); // initialize database layer $db = new patMySqlDbc("localhost", "db211", "us111", "secret"); // initialize patUser $u = new patUser(true); // connect patUser to database $u->setAuthDbc($db); // set tables $u->setAuthTable("users"); // add user $u->addUser( array("username" => "tom", "passwd" => "tom") ); // add user again $u->addUser( array("username" => "tom", "passwd" => "tom") ); // get message and display $e = $u->getLastError(); echo "Operation failed. " . $e['message'] . " (#" . $e['code'] . ")"; ?>
You can obtain a complete list of all the errors in the stack via the getAllErrorCodes() and getAllErrorMessages() methods, which return arrays of all the error codes and messages generated by patUser during the execution of the script, or again use the equivalent shortcut method getAllErrors() to get both codes and messages in one pass. A complete list of possible error codes and what they mean is available in the patUser documentation.

 
 
>>> More PHP Articles          >>> More By icarus, (c) Melonfire
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: