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

A Well-Formed Plan - PHP

In this second part, find out how you can use the patUser API toview, add, edit and delete users (and user attributes) from your userdatabase.

TABLE OF CONTENTS:
  1. User Authentication With patUser (part 2)
  2. Meeting The Family
  3. Asking For More
  4. Drilling Deeper
  5. All For One, And One For All
  6. Accounting For Change
  7. California Calling
  8. Making New Friends
  9. A Fast Edit
  10. Here Today, Gone Tomorrow
  11. Connecting The Dots
  12. A Well-Formed Plan
  13. Slice And Dice
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 7
May 01, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
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
 

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: