HomePHP Page 5 - User Authentication With patUser (part 3)
No Distinguishing Marks - PHP
In this concluding article, find out about how to use patUser toidentify users and groups by different criteria, track a user's clicks, maintain user statistics, and gracefully handle errors.
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.