User Authentication With patUser (part 2) - Making New Friends (
Page 8 of 13 )
It's just as simple to add new users and groups to the
system - all you need to do is use patUser's addUser() and addGroup() methods,
which accept an array of field-value pairs and uses them to create a new user or
group record in the database. Both methods return the ID of the newly-created
user or group if successful. Consider the following example, which demonstrates
how to add new users:
<?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 table
$u->setAuthTable("users");
// add user
$uid = $u->addUser( array("username" => "tom", "passwd" => "tom") );
// check to see if user added and display message
if ($uid)
{
echo "User successfully added with UID $uid";
}
else
{
echo "User could not be added!";
}
?>
By default, patUser automatically logs the newly-created user into the
system. You can avoid this by specifying a second argument to addUser(), as in
the example below:
<?php
$u->addUser( array("username" => "tom", "passwd" => "tom"), false );
?>
In a similar manner, a new group may be added - here's how:
<?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 table
$u->setGroupTable("groups");
// add group
$gid = $u->addGroup( array("name" => "Human Resources") );
// check to see if group added and display message
if ($gid)
{
echo "Group successfully added with GID $gid";
}
else
{
echo "Group could not be added!";
}
?>