PHP
  Home arrow PHP arrow Page 12 - 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 2)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 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:
      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 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

    - 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
    - Handling Attachments in MIME Email with PHP
    - Completing the Project Management Application





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