PHP and MySQL Code

Start typing to search for PHP and MySQL Code Snippets and Articles Search

Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
Title : Use of bitmasks to represent permissions
Categories : PHP, Authentication, Bitwise Operators, Security, PHP Classes Click here to Update Your Picture
Keith Ganger
Date : Mar 04th 2004
Grade : 4 of 5 (graded 5 times)
Viewed : 32109
File : 3809.php
Images : No Images for this code example.
Search : More code by Keith Ganger
Action : Grade This Code Example
Tools : My Examples List

Submit your own code examples  Submit your own code examples 

This class uses a bit mask to represent user permissions.
This will allow you to store a singe integer representing a users permission set.
You can then covert this integer back into an associative array with boolean
values showing the users permission set.
@author Keith Ganger <[email protected]>
@version 1.0
@since March 3 2004

class permissions
     * This array is used to represent the users permission in usable format. 
     *You can change remove or add valuesto suit your needs. 
     *Just ensure that each element defaults to false. Once you have started storing
     *users permsisions a change to the order of this array will cause the
     *permissions to be incorectly interpreted. 
     * @type Associtive array
var $permissions=array("read"=>false,"write"=>false,"delete"=>false,"change_permissions"=>false,"admin"=>false);
    *This function will use an integer bitmask(as created by toBitmask())
    *to populate the class vaiable
    *$this->permissions with the users permissions as boolean values.
    *@param int $bitmask an integer representation of the users permisions.
    *This integer is created by toBitmask();
    *@return an associatve array with the users permissions.
function getPermissions($bitMask=0){
        *The following explains how this code works.
        *This table shows how bitmasks will represent a particular permission.
        *element bin number -- 2^i -- decimal equiv
        *read     00000001   -- 2^0 -- 1
        *write   00000010   -- 2^1 -- 2
        *delete  00000100   -- 2^2 -- 4
        *The following code block loops through the permissions it uses a bitwise AND(&)
        *along with the terinary operator to assign the permissions.
        *You may want to visit the documentation at on the pow function,
        *the terinary operator, and the bitwise AND(&).
        *When using the bitwise AND(&) all bits that are set in both $bitMask and the
        *return value of pow(2($i) are set to 1.
        *For this example refer to the above table 
        *A user with read and delete permissions would be represented as 00000101
        *which would be an integer bitmask of 5
        *when this code is executed the following would happen.
        *The bitmask of 5 would be passed into the function
        *so $bitmask is set to 5 or in binary(00000101).
        *The first time through the loop the function pow(2,0) returns 1 (00000001)
        *the bitwise AND(&) then compares
        *(00000001) to (00000101) and returns 00000001 this is not equal to 0
        *so the first element in the array "read" is set to true.
        *The second time through the loop the function pow(2,1) returns 2 (00000010)
        *the bitwise AND(&) then compares
        *(00000010) to (00000101) and returns 00000000 this is equal to 0
        *so the second element in the array "write" is set to false.
        **The second time through the loop the function pow(2,2) returns 4 (00000100)
        *the bitwise AND(&) then compares
        *(00000100) to (00000101) and returns 00000100 this is 4 and is not equal to 0
        *so the third element in the array "delete" is set to false.
        *The code will loop through the remaining elements setting all of them to false.
$this->permissions as $key=>$value){
$this->permissions[$key]= (($bitMask & pow(2,$i)) !=0) ? true: false;
//uncomment the next line if you would like to see what is happening.
            //echo $key . " i= ".strval($i)." power=" . strval(pow(2,$i)). "bitwise & = " . strval($bitMask & pow(2,$i))."<br>";
/**This function will create and return and integer bitmask based on the permission values set in
    *the class variable $permissions. To use you would want to set the fields in $permissions to true for the permissions you want to grant.
    *Then call toBitmask() and store the integer value.  Later you can pass that integer into getPermissions() to convert it back to an assoicative
    *@return int an integer bitmask represeting the users permission set.
function toBitmask(){
$this->permissions as $key=>$value){

//this code demonstrates how to use the class
//instanciate the class.
$perms = new permissions();

//the first example shows how you would use the class to set permissions for a user
//echo "bitmask for read and delete= ". $bitmask ."<br>";
$sql ="insert into user_permissions (userid,docid,permission) values($userid,$docid,$bitmask)";
//you would then execute code to insert your sql.

//the second example shows you how to use the bitmask to get the users permissions.
//you would execute some sql to retrieve the bitmask from the database like
//select bitmask from user_permissions where docid=$docid and userid=$userid
//$bitmask = $row["bitmask"];
//I will just set this to a hardcoded value of 5 (as used in all the examples
"read permissions<br>";
//print array to show values


A damaged image generator (class) for validating text. CAPTCHA - Completely Automated Public Turing test to tell Computers and Humans Apart
Categories : PHP, PHP Classes, Security, GD image library, Security
Antispoof - a class to help prevent people hi-jacking and misusing parts of a website
Categories : PHP, PHP Classes, Security
Function to generate readable/remeberable random password
Categories : PHP, Security, Security
Access_user Class - an easy to use system for protecting pages and register users.
Categories : PHP, Classes and Objects, Object Oriented, PHP Classes, Authentication
.htpassword manager for apache
Categories : PHP, PHP Classes, Authentication, Apache
Password using php, Javascript, and html form
Categories : Security, PHP, Authentication, Java Script
Forms protected from XSS attacks (FOPAXSS)
Categories : PHP, PHP Classes, Form Processing, Security
Password protection for Phorum 3.1.x with userlevels and log.
Categories : PHP, MySQL, Authentication, Security
Simple and fast user authentication
Categories : PHP, PHP Classes, Authentication
Form Security - Match A Value For Success
Categories : PHP, Authentication, HTML and PHP, Sessions, Security
Generating and Matching Secure and Strong Password Hash
Categories : PHP, PHP Classes, Cryptography, Security
Authenticator for Exchange Server LDAP
Categories : PHP, Authentication, LDAP, Security, Sessions
Password Creator: This PHP code exmaple shows how to use bitwise operations on a single variable and using it as a flagged variable. The class generates passwords of a given length using specified characters and the flags.
Categories : PHP, PHP Classes, Algorithms, Security
phpSecurePages is a PHP module to secures pages with a loginname and password. It handles multiple user groups (each has own viewing rights), store data in a MySQL database or a configuration file, and can be used to identify Web site viewers.
Categories : PHP, Security, Authentication
Simple Password example
Categories : PHP, Authentication, Security, HTTP
 Jose Santos wrote : 1058
Permissions is very useful in UNIX / LINUX !!
So that this examnple, is very important, when the server is UNIX / LINUX, to change or get permissions.
In UNIX / LINUX, permissions are for example:
   -rwxrwxrwx notes.txt =&gt; that to map over binary 111111111 or decimal 777
   -rwxr-xr-x script =&gt; that to map over binary 111101101 or decimal 755

 Jon Ellis-Jones wrote :1223
nicely done.  thanks for the example code, it`s a useful representation.