PHP
  Home arrow PHP arrow Page 12 - User Authentication With patUser (part 2)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 7
    2003-05-01


    Table of Contents:
  • User Authentication With patUser (part 2)
  • Meeting The Family
  • Asking For More
  • Drilling Deeper
  • All For One, And One For All
  • Accounting For Change
  • California Calling
  • Making New Friends
  • A Fast Edit
  • Here Today, Gone Tomorrow
  • Connecting The Dots
  • A Well-Formed Plan
  • Slice And Dice

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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 2) - A Well-Formed Plan
    ( Page 12 of 13 )

    Now, how about a couple of examples to put all this in context? This first example demonstrates how the various user and group manipulation methods discussed in this article can be used to rapidly build a user administration module for a Web application or Web site. Consider the following script, which is designed to allow administrators to add new users to the system.


    <?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/template engines $u->setAuthDbc($db); // set tables $u->setAuthTable("users"); $u->setGroupTable("groups"); $u->setGroupRelTable("usergroups"); ?> <html> <head> <basefont face="Arial"> </head> <body> <?php // display initial form if (!$_POST['submit']) { ?> <h2>New User</h2> <form action="<?=$ME?>" method="post"> First name <br> <input type="text" name="fname" size="10"> <p> Last name <br> <input type="text" name="lname" size="10"> <p> Username <br> <input type="text" name="username" size="10"> <p> Password <br> <input type="password" name="passwd" size="10"> <p> Email address <br> <input type="text" name="email" size="25"> <p> Department <br> <select name="gid[]" multiple> <?php // get group list // display as multi-select box $groups = $u->getGroups( array("gid", "name") ); foreach ($groups as $g) { ?> <option value="<?=$g['gid']?>"><?=$g['name']?></option> <?php } ?> </select> <p> <input type="submit" name="submit" value="submit"> </form> <? } else { // if form submitted // validate form data // ideally you would want more stringent validation rules here! if (!$_POST['fname']) { echo "First name not entered!"; die; } if (!$_POST['lname']) { echo "Last name not entered!"; die; } if (!$_POST['username']) { echo "Username not entered!"; die; } if (!$_POST['passwd']) { echo "Password not entered!"; die; } if (!$_POST['email']) { echo "Email address not entered!"; die; } if (sizeof($_POST['gid']) == 0) { echo "Department not selected!"; die; } // if data OK, add user $uid = $u->addUser( array( "username" => $_POST['username'], "passwd" => $_POST['passwd'] ) ); // get UID if ($uid) { // add other user data $u->modifyUser( array( "username" => $_POST['username'], "passwd" => $_POST['passwd'], "fname" => $_POST['fname'], "lname" => $_POST['lname'], "email" => $_POST['email'], ) ); // add user to groups foreach ($_POST['gid'] as $g) { $u->addUserToGroup( array("uid" => $uid, "gid" => $g) ); } // display status echo "User successfully added!"; } else { // else display error echo "User could not be added!"; } } ?> </body> </html>
    This script is split into two parts. The first part is a simple HTML form containing fields for user information (including the critical username and password fields) and group membership. The list of groups is obtained from the system itself, via a call to the getGroups() method. An administrator may fill up this form with the components of a user record, and also attach a user to one or more of the named groups.

    Once the form has been submitted and the data within it validated, the addUser() method is invoked to add the user to the system. The unique user ID returned by the addUser() method can then be used by the modifyUser() and addUserToGroup() methods to set up the rest of the user record and group memberships.

    You'll notice that, unlike traditional scripts of this nature, there are no SQL queries in the code above. This is because patUser does most of the heavy lifting for you, encapsulating all needed queries within its user management API; all you need to do is invoke the appropriate method and pass it the data that needs to be entered into the database. Integrating patUser, therefore, can substantially reduce the time you spend on building such utility scripts.

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

       

    PHP ARTICLES

    - Building Dynamic Queries with Chainable Meth...
    - PHP Encryption and Decryption Methods
    - Building a MySQL Abstraction Class with Meth...
    - Completing a Sample String Processor with Me...
    - Mastering WHILE Loops for PHP and MySQL
    - Method Chaining: Adding More Methods to the ...
    - Method Chaining in PHP 5
    - The Role of Interfaces in Applying the Depen...
    - Dependency Injection: Using a Setter Method ...
    - Using a Model Class with the Dependency Inje...
    - Injecting Objects Using Setter Methods with ...
    - Injecting Objects by Constructor with the De...
    - The Dependency Injection Design Pattern in P...
    - Performing Inferential Statistical Analysis ...
    - Performing Descriptive Statistical Analysis ...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT