位运算错误?

发布于 2024-09-14 03:08:30 字数 824 浏览 6 评论 0原文

我正在开发一个有趣的网站,并且正在尝试实现基于按位运算符的目录访问控制。
我已定义 GUEST = 1、GROUP1 = 15 和 GROUP2 = 23

如果我进行比较,

echo (23 & 1); // print 1 

但如果我定义 GUESTGROUP1GROUP2

define('GUEST', 1);  
define('GROUP1', 15);  
define('GROUP2', 23);  
// and then  
echo (GROUP2 & GUEST); // print 0  
echo USER_GUEST.','.USER_ROLES1.','.USER_ROLES2; // print 1,15,23` 

使用GROUP1没问题:

echo (GROUP1 & GUEST); print 1.  

我哪里错了?有什么建议吗? 谢谢。


我发现了一些非常奇怪的事情:我的 GUEST、GROUP1 和 GROUP2 是在一个 ini 文件中声明的,我通过函数 parse_ini_file(self::$fileName, true); 解析该文件。一个班级内。 解析文件后,我递归地定义了 [DEFINE] 部分下定义的键=值对(一个简单的技巧)。 如果我在那里评论我的 GROUP2=23 定义,并在当前脚本(GROUP2 和 GUEST)中声明它,则返回 1!

I'm developping a site for fun and I'm trying to implement a directory access control based on bitwise operators.
I've defined GUEST = 1, GROUP1 = 15 and GROUP2 = 23

If I compare

echo (23 & 1); // print 1 

but if I define GUEST, GROUP1 and GROUP2:

define('GUEST', 1);  
define('GROUP1', 15);  
define('GROUP2', 23);  
// and then  
echo (GROUP2 & GUEST); // print 0  
echo USER_GUEST.','.USER_ROLES1.','.USER_ROLES2; // print 1,15,23` 

With GROUP1 no problem:

echo (GROUP1 & GUEST); print 1.  

Where do I'm wrong ? some suggestion ?
Thanks.


I've discovered something really strange: my GUEST, GROUP1 and GROUP2 are declared inside an ini file which I parse by a function parse_ini_file(self::$fileName, true); within a class .
After I've parsed the file I define recursively the couples key=value defined under section [DEFINE] (a simply trick).
If I comment there my GROUP2=23 definition and I declare it inside the current script (GROUP2 & GUEST) return 1!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

月亮邮递员 2024-09-21 03:08:30

确保您的组使用以 2 为基数的数字,即 1,2,34,8,16,32...,否则您可能会互相踩踏。这是在位字段中执行权限的正确方法。

define('GUEST', 1);  
define('GROUP1', 2);  
define('GROUP2', 4);
$groups |= GUEST;
$groups |= GROUP1;
if($groups & GUEST) {
    // This user is a guest (it is)
}
if($groups & GROUP1) {
    // This user is in group 1 (it is)
}
if($groups & GROUP2) {
    // This user is in group 2 (it is NOT)
}

Make sure you use base 2 numbers for your groups, i.e. 1,2,34,8,16,32.... or you can step on each other. Here's the correct way to do permissions in a bitfield.

define('GUEST', 1);  
define('GROUP1', 2);  
define('GROUP2', 4);
$groups |= GUEST;
$groups |= GROUP1;
if($groups & GUEST) {
    // This user is a guest (it is)
}
if($groups & GROUP1) {
    // This user is in group 1 (it is)
}
if($groups & GROUP2) {
    // This user is in group 2 (it is NOT)
}
妞丶爷亲个 2024-09-21 03:08:30

PHP 5.3:

php > define('GUEST', 1);
php > define('GROUP1', 15);
php > define('GROUP2', 23);
php > echo GROUP2 & GUEST;
1
php > echo GROUP1 & GUEST;
1

您使用的 PHP 版本是什么?


Artefacto 指出了一个可能的字符串问题(e:但似乎已经撤回了他的帖子,嗯)。再次在 5.3 中:

php > var_export(GUEST);
1
php > var_export(GROUP1);
15
php > var_export(GROUP2);
23
php > define('GUEST_AS_STRING', '1');
php > var_export(GUEST_AS_STRING);
'1'
php > echo GROUP1 & GUEST_AS_STRING;
1
php > echo GROUP2 & GUEST_AS_STRING;
1

“1”是字符 49。 49 & 15 是 1,但是 49 & 是 1。 23 是 17。我不相信这是一个字符串问题......

PHP 5.3:

php > define('GUEST', 1);
php > define('GROUP1', 15);
php > define('GROUP2', 23);
php > echo GROUP2 & GUEST;
1
php > echo GROUP1 & GUEST;
1

What PHP version are you using?


Artefacto pointed out a possible string issue (e: but appears to have retracted his post, hm). Again in 5.3:

php > var_export(GUEST);
1
php > var_export(GROUP1);
15
php > var_export(GROUP2);
23
php > define('GUEST_AS_STRING', '1');
php > var_export(GUEST_AS_STRING);
'1'
php > echo GROUP1 & GUEST_AS_STRING;
1
php > echo GROUP2 & GUEST_AS_STRING;
1

'1' is character 49. 49 & 15 is 1, but 49 & 23 is 17. I'm not convinced that this is a string issue...

捶死心动 2024-09-21 03:08:30

已解决。
我完全误解了 php 手册 中的声明,其中在 Changelog 中说:

5.2.4 由数字组成的键和节名称现在被评估为 PHP 整数,因此以 0 开头的数字被评估为八进制,而以 0x 开头的数字被评估为十六进制。

绝对清楚:键和部分名称...而不是值!
parse_ini_file() 函数将整数值计算为 PHP 字符串,即使它们没有用双引号括起来。这很遗憾,但事实就是如此;-)

感谢您的合作

Solved.
I completely misunderstood a declaration in php manual where in Changelog they say:

5.2.4 Keys and section names consisting of numbers are now evaluated as PHP integers thus numbers starting by 0 are evaluated as octals and numbers starting by 0x are evaluated as hexadecimals.

It's absolutly clear: keys and section name ... not values!
The parse_ini_file() function evaluates integers values as PHP string even if they aren't enclosed in double quotes. This is pity, but so it is ;-)

Thanks for your collaboration

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文