Set permissions in binary

前端 未结 5 1973
说谎
说谎 2020-12-29 16:06

I saw in school a system that set permissions using binary string.

Let\'s say 101001 = 41

So :

  • 1 can be permission to page 1
  • 2 can b
相关标签:
5条回答
  • 2020-12-29 16:38

    Since it's binary & decimal you can convert it back & forth with decbin and bindec. It's just a different base to count with...

    EDIT: bitwise is sweet, do that ^_^

    0 讨论(0)
  • 2020-12-29 16:40

    Here is a nice snippet. taken from here http://www.litfuel.net/tutorials/bitwise.htm

    <?php
    $read = 1;
    $write = 2;
    $readwrite = 16;
    $local_admin = 32;
    $global_admin = 64;
    
    $jim = 96;
    $mike = 16;
    
    echo "Is Mike Allowed to edit? he has an access level of 16<BR>";
    if ($mike & 32) {
        echo 'YES MIKE CAN EDIT';
    } else {
        echo 'NO HE CANNOT';
    }
    
    echo "<BR><BR>Is Jim Allowed to edit? he has an access level of 96<BR>";
    if ($jim & 32) {
        echo 'YES JIM CAN EDIT';
    } else {
        echo 'NO HE CANNOT';
    }
    ?>
    
    0 讨论(0)
  • 2020-12-29 16:45

    Sounds like you're talking about bits and bit-wise operators. This easiest way to set this up is to define constants for every permission

    const POST   = 1;
    const DELETE = 2;
    const UPDATE = 4;
    const READ   = 8;
    

    Once you have these defined it's easy to make comparisons using the bitwise operators:

    $userValue = '1101';
    
    if ($userValue & self::POST) {
      echo 'Can Post';
    }
    
    if ($userValue & self::DELETE) {
      echo 'Can Delete';
    }
    
    if ($userValue & self::UPDATE) {
      echo 'Can Update';
    }
    
    if ($userValue & self::READ) {
      echo 'Can Read';
    }
    

    This is how many of PHP's own constants work. If you've ever set the error reporting using something like E_ALL & E_DEPRECATED you're actually working with binary numbers.

    0 讨论(0)
  • 2020-12-29 16:47

    You can use & operator and check against those flags.

    $READ = 8;
    $WRITE = 1;
    $EXECUTE = 32;
    $permissions = 41;
    if ($permissions & $READ) {
       // do smth
    }
    if ($permissions & $WRITE ) {
       // do smth
    }
    if ($permissions & $EXECUTE ) {
       // do smth
    }
    

    Let me explain it. If you have bits 1001 (9). You just check forth bit (1000) with 1001. You just multiply EVERY bit of $permissions variable and bit number. Result will be 1000. It's convertible to true value. So you have flag here. If check third bit (0100) it will result in 0000 and it's false value. So you haven't got permissions here.

    0 讨论(0)
  • 2020-12-29 16:48

    That is the nature of binary - there is only one combination of ones and zeros for each number .

    A function from the PHP comments for decbin:

    function bindecValues($decimal, $reverse=false, $inverse=false) {
    /*
    1. This function takes a decimal, converts it to binary and returns the
         decimal values of each individual binary value (a 1) in the binary string.
         You can use larger decimal values if you pass them to the function as a string!
    2. The second optional parameter reverses the output.
    3. The third optional parameter inverses the binary string, eg 101 becomes 010.
    -- darkshad3 at yahoo dot com
    */
    
        $bin = decbin($decimal);
        if ($inverse) {
            $bin = str_replace("0", "x", $bin);
            $bin = str_replace("1", "0", $bin);
            $bin = str_replace("x", "1", $bin);
        }
        $total = strlen($bin);
    
        $stock = array();
    
        for ($i = 0; $i < $total; $i++) {
            if ($bin{$i} != 0) {
                $bin_2 = str_pad($bin{$i}, $total - $i, 0);
                array_push($stock, bindec($bin_2));
            }
        }
    
        $reverse ? rsort($stock):sort($stock);
        return implode(", ", $stock);
    }
    

    Usage

    $binary_array = bindecValues(41);
    

    This would make binary_array:

    array(1, 8, 32);
    
    0 讨论(0)
提交回复
热议问题