Jump to content

User Login with Activation Problem


awtprod

Recommended Posts

I'm trying to modify a login script to prevent it from users that haven't activated the account, to log in. I've got everything correct on the database side but the script still allows you to log in, even if you haven't activated yet. The code works by assigning the user to level 0 when they first register and changing it to 1 after they activate. This code is suppose to check that they are not a level 0 before allowing them to log in. I hope this makes sense. Any help will be greatly appreciated! Thanks in advance!

 

 

  function confirmUserLevel($username){
    

      /* Verify that user is activated */
      $q = "SELECT userlevel FROM ".TBL_USERS." WHERE username = '$username'";
      $result = mysql_query($q, $this->connection);
      if(mysql_fetch($result) == 0){
         return 1; //Indicates userlevel failure
         }
      else{
         return 0; //Indicates user is activated.
      }
   }

 

     /* Checks that user is activated */
      
      $result = $database->confirmUserLevel($subuser);

      /* Check error codes */
      if($result == 1){
         $field = "user";
         $form->setError($field, "* User is NOT activated");
      }
     
      
      /* Return if form errors exist */
      if($form->num_errors > 0){
         return false;
      }

Link to comment
https://forums.phpfreaks.com/topic/186191-user-login-with-activation-problem/
Share on other sites

      /* Verify that user is activated */
      $q = "SELECT userlevel FROM ".TBL_USERS." WHERE username = '$username' LIMIT 1";
      $result = mysql_query($q, $this->connection) or trigger_error("Query Failed: " . mysql_error());
      $level = mysql_result($result, 0, 0); // fetch the first column from the first row
      if($level == 1){
         return true; //Indicates user is active      
      }

      return false; //Indicates user is not activated.

 

Should work for ya, mysql_result is able to pull a single column since that is all you need. Hope that helps. You should use true (1) if the user is a valid level and false (0) if they are not. This makes a lot more sense to more people.

 

EDIT:

Fixed a syntax issue.

You could condense it somewhat by actually querying for the correct information. You don't really need the result.

 

function confirmUserLevel($username){
  $q = "SELECT userlevel FROM ".TBL_USERS." WHERE username = '$username' AND userlevel = 1";
  if ($result = mysql_query($q, $this->connection)) {
    return mysql_num_rows($result);
  }
  return false;
}

 

This will return true if the user exists and has a userlevel of 1, false otherwise.

OK. That worked. Thanks!

 

Sorry to bring up the topic again. I thought my problem was unique enough to start a new thread.

 

And, no, lol, I didn't know that 0 was false and 1 was true. I'm new at php and I don't much more than the very basic stuff.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.