


用二进制控制权限码,int类型的32位,比如总的权限码来个1000,说明第四位有权限,其他没有权限,那么我该怎么判断各位的权限码,多个权限呢?
二进制用int类型32位表示那个位有权限,下面的32位二进制表示权限码
全部有权限的话是: 11111111 11111111 11111111 11111111 //(下面的最前面的00000都是可以省略的)
只有最低位有权限:00000000 00000000 00000000 00000001
第一位有权限: 00000000 00000000 00000000 00000010
第二位有权限: 00000000 00000000 00000000 00000100
多个权限的: 00000000 00000000 00000000 00000101 //(这个是最后一位和第二位有权限)
我想问的是,假如我第零位是增加权限,第一位是查找权限,第二位是更新权限,第三位是删除权限,
但是单个权限时候我怎么判断这位有权限,或者给出的权限码是多个权限的时候我怎么能判断到底它有啥权限呢?
看看谁能写个方法,把得到的权限码放到方法里就能判断?
我这个其实要判断完是要放到前端展现到前端界面的 四个复选框
增加 删除 修改 删除 //如果后端判断的是有两个权限则用户进入界面是看到 两个复选框是直接选中的????做php做权限的卡到这里了 求大牛们指导???、??/、????
回复讨论(解决方案)
?考一下 Linux ?限的做法 777 755 .... 前端?示 根? 所具?的?限 展示就可以了.
分割,判断,应该是这两个步骤了。。
我看过的大部分权限都是 1,1,1,1,1,1,1,1这样子有分割符号好区分的。
不知道你那是咋分割的,不过要是有具体能跑起来的小实例最好,我可以比葫芦画瓢再扩展。。。。。。。。。。。
很简单,位与
$权限 = array( '增加' => 0b1, '查找' => 0b10, '更新' => 0b100, '删除' => 0b1000,);$权限字 = 0b101;foreach($权限 as $k=>$v) { printf("<input type=checkbox name=permit[] value=%s %s>%s<br>\n", $k, $权限字 & $v ? 'checked' : '', $k);}
<input type=checkbox name=permit[] value=增加 checked>增加<br><input type=checkbox name=permit[] value=查找 >查找<br><input type=checkbox name=permit[] value=更新 checked>更新<br><input type=checkbox name=permit[] value=删除 >删除<br>
$权限 = array( '增加' => 0b1, '查找' => 0b10, '更新' => 0b100, '删除' => 0b1000,);$权限字 = 0b101;foreach($权限 as $k=>$v) { printf("<input type=checkbox name=permit[] value=%s %s>%s<br>\n", $v, $权限字 & $v ? 'checked' : '', $k);}
<input type=checkbox name=permit[] value=1 checked>增加<br><input type=checkbox name=permit[] value=2 >查找<br><input type=checkbox name=permit[] value=4 checked>更新<br><input type=checkbox name=permit[] value=8 >删除<br>
考虑到多个权限的问题,pow的第二个参数可以一直向上增。不知道你看明白没有
$add=pow(2,1);//2$del=pow(2,2);//4$update=pow(2,3);//8$query=pow(2,4);//16//增加&删除$p=$add+$del;if(getstatus($p,1)){//1是查询 echo '有增加的权限<br>';}//增加&删除&修改$p=$add+$del+$update;if(getstatus($p,3)){//3是修改 echo '有修改的权限<br>';}if(!getstatus($p,4)){//4是查询 echo '没有查询的权限<br>';}function getstatus($status,$p){ $t = $status & pow(2, $p) ? 1 : 0; return $t;}
五楼的方法我试过了,我的前端代码有个引入的格式,他输出放到前端时候总是不放到格式里,不知道是怎么回事,是不是要在printf(),之前要加点代码什么的,前台我的html代码如下:
、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、
查看网页源码时候 他们这些
查看网页源码时候,增加
查找
更新
删除
不在这个块中??求解
你现在应该是可以设置32种权限,判断单个标志位是否有这个权限
getAuth($postStatus, $targetStatus){ return $status & $targetStatus;}getAuth(0x21f, 0x78); //判断是否有 00000000 00000000 00000000 011111000权限
php里这种做法弊大于利吧,从程序角度来看,貌似没有起到什么优化作用;从数据结构来看,也不方便管理和查找
个人想法

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

Using preprocessing statements and PDO in PHP can effectively prevent SQL injection attacks. 1) Use PDO to connect to the database and set the error mode. 2) Create preprocessing statements through the prepare method and pass data using placeholders and execute methods. 3) Process query results and ensure the security and performance of the code.
