PHP
  Home arrow PHP arrow Page 2 - User Authentication With patUser (part...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Moblin 
JMSL Numerical Library 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
PHP

User Authentication With patUser (part 3)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 5
    2003-05-07

    Table of Contents:
  • User Authentication With patUser (part 3)
  • Making Exceptions
  • The History Channel
  • Natural Selection
  • No Distinguishing Marks
  • Big Brother Is Watching
  • Endgame

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    User Authentication With patUser (part 3) - Making Exceptions


    (Page 2 of 7 )

    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


     

       

    PHP ARTICLES

    - Validating Web Forms with the Code Igniter P...
    - Output Buffering
    - Paginating Database Records with the Code Ig...
    - HTTP Headers in Web Development
    - Project Management: Administration
    - Building a Database-Driven Application with ...
    - User Authentication for a Project Management...
    - Introduction to the CodeIgniter PHP Framework
    - Adding Users for a Project Management Applic...
    - Migrating Class Code for a MIME Email to PHP...
    - Login and Logout Authentication for a Projec...
    - Composing Messages in HTML for MIME Email wi...
    - Project Management: Authentication
    - A Better Way to Determine MIME Types for MIM...
    - Project Management Overview





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway