home support FAQ resources services partners contact us contact us
 Web Application Previous  Next  
 Tutorial

Listing 6.6 Using the Perm class.

page_open(array(“sess” => “Example_Session”, “auth” => “Example_Auth”,
➥”perm” => “Example_Perm”));
if(isset($mode) && $mode == “reload”)
{
$auth->unauth();
print(“You have been logged out.<br>”);
printf(‘If you want, you can <a href=”%s”> login again.’,
➥$sess->url(basename($PHP_SELF)));
}
else
{
if($perm->have_perm(“admin”))
{
print(“<b>Welcome Admin.</b><br>”);
print(‘You are logged in with “admin” permissions.<br>’);
}
else
{
printf(‘You are logged in with “%s” permissions.<br>’,
➥$auth->auth[“perm”]);
}
printf(“Your user name: %s<br>”, $auth->auth[“uname”]);
printf(‘<a href=”%s”>Log out</a>’,
➥$sess->url(basename($PHP_SELF).”?mode=reload”));
}
page_close();
Bit Bashing
Bitwise calculations often cause a great deal of confusion among novice programmers,
and even advanced developers have occasional difficulties dealing with them.
Representing flag values as bit patterns can be very useful, though; the PHPLib uses
this for permission levels. It’s also used often to store flag values in a single INT field of
a database. Consider an application that needs to keep track of a number of different
states, for example users’ hobbies. Instead of having one field in a database for each
hobby and setting that to true or false, you can have a single flag field. Depending
on whether the user has a specific hobby, this hobby’s bit is switched on or off.
Simply put, bitwise operations are operations that manipulate one or more bits at a
time.You know that in the binary system, you have octets of bits— a series of 0 and 1.
The decimal number 42 in the binary system is represented as 00101010:

Bit position: 7 6 5 4 3 2 1 0
Bit value: 0 0 1 0 1 0 1 0
Previous  Next  
Link Partners: Asia florist, Flowers to India, Hong kong flowers, Site submit, Cheap web hosting, China florist, Japan florist