|  | Posted by Tom Thackrey on 07/08/05 21:01 
On  8-Jul-2005, "Michael B. Trausch" <fdZEROman+spam@gmail.nospam.com>wrote:
 
 > I have a question regarding bitwise operators, I've been trying to
 > figure this out for about two days now, and I just can't seem to get it.
 >    What I'm trying to do is use a variable to hold a bitmask of 'flags'
 > for users on a website.  The function below is supposed to be a search
 > function for one of those flags in a particular thing.
 >
 > The idea is that when something calls userId(), it should get to see
 > what type of user flags that are supported.  Now I've moved things
 > around, and it seems like the only if that ever executes is the first
 > one, the code doesn't ever reach any of the elseif blocks.
 >
 > I want the function to search for the first user that it finds has a bit
 > set in the bitmask and return the userId as supplied by the cookie that
 > represents that bitmask.
 >
 > Optionally, userId() can be called with one of the arguments specifying
 > a particular usertype to look for that particular usertype.  The user
 > can be logged into multiple sections of the site at a time, so if
 > they're entering something where they need the USER_ADMIN privilege, I
 > should be able to call userId(USER_ADMIN) and get their UID if they're
 > an admin and E_NOT_LOGGED_IN if they're not logged in as an admin.  Same
 > goes for any of the options that are defined.  Can anyone else see what
 > is wrong with this?
 >
 > Thanks for the help in advance.
 >
 >
 > Constants defined for USER_*:
 > =============================
 >
 > define('USER_NOBODY',   0x00000000);
 > define('USER_SEEKER',   0x00000001);
 > define('USER_MEMPL',    0x00000002);
 > define('USER_SEMPL',    0x00000004);
 > define('USER_ADMIN',    0x00000008);
 > define('USER_ANYBODY',  0xffffffff);
 >
 > Code that isn't working:
 > ========================
 >
 > function userId($pref = USER_ANYBODY)
 > {
 >     /*
 >      * If the user is logged in, this will return their user ID.
 >      * If they are not, then it won't.  If more then one user is
 >      * logged in, this function will prefer MEMPL and SEMPL types
 >      * over any other, unless specified by $pref.
 >      */
 >
 >     $uT = userType();
 >
 >     if(userAccess($uT, $pref))
 >     {
 >         /*
 >          * The usertype that we're looking for is logged in.
 >          */
 >
 >         if(($pref & USER_SEEKER) > 0)
 >         {
 >             $tmpCookie = $_COOKIE['seeker'];
 >             list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);
 >
 >             return($id);
 >         }
 >
 >         elseif(($pref & USER_MEMPL) > 0)
 >         {
 >             $tmpCookie = $_COOKIE['mEmpl'];
 >             list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);
 >
 >             return($id);
 >         }
 >
 >         elseif(($pref & USER_SEMPL) > 0)
 >         {
 >             $tmpCookie = $_COOKIE['sEmpl'];
 >             list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);
 >
 >             return($id);
 >         }
 >
 >         elseif(($pref & USER_ADMIN) > 0)
 >         {
 >             $tmpCookie = $_COOKIE['admin'];
 >             list($id, $i, $i, $i, $i, $i, $i) = explode(':', $tmpCookie);
 >
 >             return($id);
 >         }
 >     }
 >
 >     // Oops.  We don't have anything to do, return E_NOT_LOGGED_IN.
 >
 >     return(E_NOT_LOGGED_IN);
 > }
 
 The problem is you aren't using the bitwise operators. & is not bitwise, &&
 is
 
 Also, you don't need the >0, if ($pref && USER_ADMIN) will work just fine
 and won't have a problem if you use the high order bit as a flag.
 
 --
 Tom Thackrey
 www.creative-light.com
 tom (at) creative (dash) light (dot) com
 do NOT send email to jamesbutler@willglen.net (it's reserved for spammers)
  Navigation: [Reply to this message] |