This forum is in archive mode. You will not be able to post new content.

Author Topic: [PHP] Bitwise Right systems  (Read 3729 times)

0 Members and 1 Guest are viewing this topic.

Offline Stackprotector

  • Administrator
  • Titan
  • *
  • Posts: 2515
  • Cookies: 205
    • View Profile
[PHP] Bitwise Right systems
« on: December 14, 2012, 10:56:53 PM »
Hello :)


This little tutorial is supported by beer :D , credits to the beer.


Most of the PHP developers have seen right systems in forums, where for example a moderator has 1435 rights. And most of you (like me) wonder why. Also.. most of you know a little of binary (http://en.wikipedia.org/wiki/Binary_numeral_system). In this tutorial i will give you a little example how to use binary in php.


To give you a little introduction into binary: Binary is a 2 digit numeral system. In the most communities we use a 10 digit numeral system, that is 1,2,3,4,5,6,7,8,9,10. Because computers are made using ON and OF systems it only uses 2 digits, being 0,1.



Try yourself: to calculate in a numeral system use the following piece of math NUMBER*NUMBERSINSYSTEM^LOCATIONOFNUMBER. So for example to calculate the numer 5 we would do: 5 times 10 in the power of 0 (5*10^0). If we'd like to calculate the binary example of 0010 we would do 0*2^0 + 1*2^1 = 2. Where "0*2^0" is equals to the first 0 and " 1*2^1" to the first occurring 1.
WOW now you know why those crazy developers use "0" in an array :)

I hope you got the piece above :) , if not GOOGLE.COM  (or post a reply)  :D .


Okay.. lets get to the point.


I have the following array including the pre-defined rights :
Code: (php) [Select]

    private $rights = array('admin' => 1,
        'editor' => 2,
        'developer' => 4,
        'database' =>;


All those numbers (1,2,4,8) are unique in the binary system. And a combination of these are used to create other numbers like 10 (2+8 =10).  The bits go up to infinite but i keep it on 8 in this example.
My user is "Boobies" i want "Boobies" to edit articles and have database access. So i give "Boobies" a right var. of editor=2 + database=8 = 10. Now "Boobies" got an rights column in the database of "10". Most of you would think; WTF, okay he got 10, but how to calculate this in a reverse order, to extract the specific rights. This is where the power of the bits in the binary system comes in place. Because 0010 can only refer to 5, and 5 to binary can only refer to 0010 (10, 010, 0010). We can use the predefined "binary numbers" to extract rights out of the "rights variable".


So for example, 10 in binary can only refer to 8 and 2. Because the only numbers beneath 10 in the binary numeral system are 1 , 2, 4, 8. 


Then.. if i am going to check if a user has "edit article" rights i will do "10"(users boobies rights) AND 2. (see wikipedia for logic gates if you do not know AND,OR,XOR,XAND etc. and http://php.net/manual/en/language.operators.bitwise.php). Because 2 is a unique bit in 10 (binary) this is true. and 2+2+6 being just stupid.


Let me just say, fuck beer if you don't understand a fuck of this. I will update this in correct English and making sense language :) .


BTW: http://usnews.nbcnews.com/_news/2012/12/14/15907407-elementary-school-massacre-27-killed-including-20-kids-at-connecticut-school?lite  Fuck programming and lets study psychology and do something about people like this :(

« Last Edit: December 14, 2012, 10:59:56 PM by Factionwars »
~Factionwars

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.