search
HomeBackend DevelopmentPHP Tutorial用二进制控制权限码,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里这种做法弊大于利吧,从程序角度来看,貌似没有起到什么优化作用;从数据结构来看,也不方便管理和查找
个人想法

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
PHP Performance Tuning for High Traffic WebsitesPHP Performance Tuning for High Traffic WebsitesMay 14, 2025 am 12:13 AM

ThesecrettokeepingaPHP-poweredwebsiterunningsmoothlyunderheavyloadinvolvesseveralkeystrategies:1)ImplementopcodecachingwithOPcachetoreducescriptexecutiontime,2)UsedatabasequerycachingwithRedistolessendatabaseload,3)LeverageCDNslikeCloudflareforservin

Dependency Injection in PHP: Code Examples for BeginnersDependency Injection in PHP: Code Examples for BeginnersMay 14, 2025 am 12:08 AM

You should care about DependencyInjection(DI) because it makes your code clearer and easier to maintain. 1) DI makes it more modular by decoupling classes, 2) improves the convenience of testing and code flexibility, 3) Use DI containers to manage complex dependencies, but pay attention to performance impact and circular dependencies, 4) The best practice is to rely on abstract interfaces to achieve loose coupling.

PHP Performance: is it possible to optimize the application?PHP Performance: is it possible to optimize the application?May 14, 2025 am 12:04 AM

Yes,optimizingaPHPapplicationispossibleandessential.1)ImplementcachingusingAPCutoreducedatabaseload.2)Optimizedatabaseswithindexing,efficientqueries,andconnectionpooling.3)Enhancecodewithbuilt-infunctions,avoidingglobalvariables,andusingopcodecaching

PHP Performance Optimization: The Ultimate GuidePHP Performance Optimization: The Ultimate GuideMay 14, 2025 am 12:02 AM

ThekeystrategiestosignificantlyboostPHPapplicationperformanceare:1)UseopcodecachinglikeOPcachetoreduceexecutiontime,2)Optimizedatabaseinteractionswithpreparedstatementsandproperindexing,3)ConfigurewebserverslikeNginxwithPHP-FPMforbetterperformance,4)

PHP Dependency Injection Container: A Quick StartPHP Dependency Injection Container: A Quick StartMay 13, 2025 am 12:11 AM

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Dependency Injection vs. Service Locator in PHPDependency Injection vs. Service Locator in PHPMay 13, 2025 am 12:10 AM

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHP performance optimization strategies.PHP performance optimization strategies.May 13, 2025 am 12:06 AM

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHP Email Validation: Ensuring Emails Are Sent CorrectlyPHP Email Validation: Ensuring Emails Are Sent CorrectlyMay 13, 2025 am 12:06 AM

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Article

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor