User Authentication With patUser (part 3) - No Distinguishing Marks
(Page 5 of 7 )
That isn't all, though - patUser also comes with two other functions, identifyUser() and identifyGroup(), which allow you to identify users and groups by other criteria. 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");
// get UID
$uid = $u->identifyUser(array("email" => "tom@some-domain.com")); echo
$uid;
?>
This method is essentially a simpler version of the
getUsers() method; it checks the user database for matching records and returns the user ID of the found user if available.
You can add as many search criteria as you like, as in the following (equivalent) example:
<?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");
// get UID
$uid = $u->identifyUser(array("email" => "tom@some-domain.com",
"sex" => "M", "age" => 21, "time_online" => 0)); echo $uid;
?>
Similarly, there's also an identifyGroup() method, which can
be used to quickly look up a group ID, given the group name (or any other group attribute). Take a look:
<?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->setGroupTable("groups");
// get GID
$gid = $u->identifyGroup(array("name" => "Operations"));
echo $gid;
?>
If no user or group can be found matching the specified
criteria, both functions will return false.
Next: Big Brother Is Watching >>
More PHP Articles
More By icarus, (c) Melonfire