search
HomeBackend DevelopmentPHP Tutorial用二进制控制权限码,int类型的32位,比如总的权限码来个1000,说明第四位有权限,其他没有权限,那么小弟我该如何判断各位的权限码,多个权限呢

用二进制控制权限码,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(<br />  '增加' => 0b1,<br />  '查找' => 0b10,<br />  '更新' => 0b100,<br />  '删除' => 0b1000,<br />);<br /><br />$权限字 = 0b101;<br /><br /><br />foreach($权限 as $k=>$v) {<br />  printf("<input type=checkbox name=permit[] value=%s %s>%s<br>\n", $k, $权限字 & $v ? 'checked' : '', $k);<br />}
<input type=checkbox name=permit[] value=增加 checked>增加<br><br /><input type=checkbox name=permit[] value=查找 >查找<br><br /><input type=checkbox name=permit[] value=更新 checked>更新<br><br /><input type=checkbox name=permit[] value=删除 >删除<br><br /><br />

------解决思路----------------------
$权限 = array(<br />  '增加' => 0b1,<br />  '查找' => 0b10,<br />  '更新' => 0b100,<br />  '删除' => 0b1000,<br />);<br /><br />$权限字 = 0b101;<br /><br />foreach($权限 as $k=>$v) {<br />  printf("<input type=checkbox name=permit[] value=%s %s>%s<br>\n", $v, $权限字 & $v ? 'checked' : '', $k);<br />}
<input type=checkbox name=permit[] value=1 checked>增加<br><br /><input type=checkbox name=permit[] value=2 >查找<br><br /><input type=checkbox name=permit[] value=4 checked>更新<br><br /><input type=checkbox name=permit[] value=8 >删除<br><br /><br />

------解决思路----------------------
考虑到多个权限的问题,pow的第二个参数可以一直向上增。不知道你看明白没有
<br />$add=pow(2,1);//2<br />$del=pow(2,2);//4<br />$update=pow(2,3);//8<br />$query=pow(2,4);//16<br /><br />//增加&删除<br />$p=$add+$del;<br />if(getstatus($p,1)){//1是查询<br />	echo '有增加的权限<br>';<br />}<br />//增加&删除&修改<br />$p=$add+$del+$update;<br />if(getstatus($p,3)){//3是修改<br />	echo '有修改的权限<br>';<br />}<br />if(!getstatus($p,4)){//4是查询<br />	echo '没有查询的权限<br>';<br />}<br /><br />function getstatus($status,$p){<br />	$t = $status & pow(2, $p) ? 1 : 0;<br />	return $t;<br />}<br />

------解决思路----------------------
你现在应该是可以设置32种权限,判断单个标志位是否有这个权限
<br />getAuth($postStatus, $targetStatus){<br />    return $status & $targetStatus;<br />}<br />getAuth(0x21f, 0x78);    //判断是否有 00000000 00000000 00000000 011111000权限<br />

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
What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

How do you optimize PHP applications for performance?How do you optimize PHP applications for performance?May 08, 2025 am 12:08 AM

TooptimizePHPapplicationsforperformance,usecaching,databaseoptimization,opcodecaching,andserverconfiguration.1)ImplementcachingwithAPCutoreducedatafetchtimes.2)Optimizedatabasesbyindexing,balancingreadandwriteoperations.3)EnableOPcachetoavoidrecompil

What is dependency injection in PHP?What is dependency injection in PHP?May 07, 2025 pm 03:09 PM

DependencyinjectioninPHPisadesignpatternthatenhancesflexibility,testability,andmaintainabilitybyprovidingexternaldependenciestoclasses.Itallowsforloosecoupling,easiertestingthroughmocking,andmodulardesign,butrequirescarefulstructuringtoavoidover-inje

Best PHP Performance Optimization TechniquesBest PHP Performance Optimization TechniquesMay 07, 2025 pm 03:05 PM

PHP performance optimization can be achieved through the following steps: 1) use require_once or include_once on the top of the script to reduce the number of file loads; 2) use preprocessing statements and batch processing to reduce the number of database queries; 3) configure OPcache for opcode cache; 4) enable and configure PHP-FPM optimization process management; 5) use CDN to distribute static resources; 6) use Xdebug or Blackfire for code performance analysis; 7) select efficient data structures such as arrays; 8) write modular code for optimization execution.

PHP Performance Optimization: Using Opcode CachingPHP Performance Optimization: Using Opcode CachingMay 07, 2025 pm 02:49 PM

OpcodecachingsignificantlyimprovesPHPperformancebycachingcompiledcode,reducingserverloadandresponsetimes.1)ItstorescompiledPHPcodeinmemory,bypassingparsingandcompiling.2)UseOPcachebysettingparametersinphp.ini,likememoryconsumptionandscriptlimits.3)Ad

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 Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.