note 89146 added to language.operators.bitwise
| From: | (vb[at]bertola.eu) | Date: | Tue, 24 Feb 2009 08:04:37 +0000 |
| Subject: | note 89146 added to language.operators.bitwise | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151049@lists.php.net to get a copy of this message | ||
I had a weird problem with bitwise operators applied to session variables in $_SESSION.
I would check whether a logged user was authorized to perform an operation through a simple
comparison between the bitmask of auth levels authorized for the operation and the user's auth
level stored in $_SESSION:
<?php $ok = ($allowed_auth & $_SESSION['auth']); ?>
However, this got any sort of wrong and unpredictable results!
In the end, I decided that something had to be going wrong with the way the PHP interpreter
understood this operation, and I tried to typecast the session variable... et voilà :
<?php $ok = ($allowed_auth & ((int) $_SESSION['auth'])); ?>
This works perfectly.
Bottom line: unless this is a bug that has been fixed in recent versions of PHP, you should typecast
session variables to int before performing bitwise operations on them.
----
Server IP: 80.241.173.254
Probable Submitter: 84.220.166.138
----
Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php
Edit -- https://master.php.net/note/edit/89146
Del: integrated -- https://master.php.net/note/delete/89146/integrated
Del: useless -- https://master.php.net/note/delete/89146/useless
Del: bad code -- https://master.php.net/note/delete/89146/bad+code
Del: spam -- https://master.php.net/note/delete/89146/spam
Del: non-english -- https://master.php.net/note/delete/89146/non-english
Del: in docs -- https://master.php.net/note/delete/89146/in+docs
Del: other reasons-- https://master.php.net/note/delete/89146
Reject -- https://master.php.net/note/reject/89146
Search -- https://master.php.net/manage/user-notes.php