#native_company# #native_desc#
#native_cta#

Session Management and Authentication with PHPLIB Page 5

By Chad Cunningham
on July 30, 2000

Unlike check, have_perm returns true or false, and does not exit the
script. You can use this for conditional testing, such as:

<?php

if ($perm->have_perm("guest")) {

    
//do something;

} elseif ($perm->have_perm("admin")) {

    
//do something else;

} else {

    
//yet something else;

}

?>



Database Abstraction

As mentioned earlier, PHPLIB uses a database abstraction layer. This is
required for use with the other classes (authentication, session), but is
also extremely handy to use on your own. It allows you to use a set of
arbitrary database commands which will then be mapped to the database you
are using. The benefit of this is that your apps can work with any
database simply by changing your local configuration. Here’s a simple
example to query a database.

<?php

$db = new DB_MyDBClass;

$query "select name from sometable";

$db->query($query);

while (
$db->next_record()) {

    
$db->p("name");

}

?>



The first line creates a new database object. The username, password,
host, and database are set in your local configuration. The third line then
queries the database, and stores the result set in the database object.
The $db->next_record() advances result set to the next row. The
$db->p("name") prints out the value of the field name. You can also say
$db->f("name") which simply returns the value rather than printing it out.
The above is equivalent to this chunk of mysql specific code, and also
automatically provides error handling and centralized storage of connection
data:

<?php

mysql_connect("localhost","username","password");

mysql_select_db("some_db");

$query "select name from sometable";

$result mysql_query($query);

while (
$row mysql_fetch_array($result)) {

    
$row["name"];

}

?>



 

Conclusion

There is a lot more to what PHPLIB can do, this is just an introduction.
There are more functions and classes you’ll want to look at, so head over
to http://phplib.netuse.de and check
out the docs. Check out the showroom
to get an idea of how to put together a system.
–Chad

1
|
2
|
3
|
4
|
5