awtprod Posted December 23, 2009 Share Posted December 23, 2009 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; } Quote Link to comment https://forums.phpfreaks.com/topic/186191-user-login-with-activation-problem/ Share on other sites More sharing options...
trq Posted December 23, 2009 Share Posted December 23, 2009 Another one? I had one of you guys yesterday. You do realize that 0 is considered false and 1 considered true in php? Your logic is completely backwards. edit: Sorry, I should add a smiley at least I'm not trying to be mean, just pointing it out to you. Quote Link to comment https://forums.phpfreaks.com/topic/186191-user-login-with-activation-problem/#findComment-983339 Share on other sites More sharing options...
premiso Posted December 23, 2009 Share Posted December 23, 2009 /* 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. Quote Link to comment https://forums.phpfreaks.com/topic/186191-user-login-with-activation-problem/#findComment-983343 Share on other sites More sharing options...
trq Posted December 23, 2009 Share Posted December 23, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/186191-user-login-with-activation-problem/#findComment-983344 Share on other sites More sharing options...
awtprod Posted December 24, 2009 Author Share Posted December 24, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/186191-user-login-with-activation-problem/#findComment-983488 Share on other sites More sharing options...
trq Posted December 24, 2009 Share Posted December 24, 2009 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. booleans. Quote Link to comment https://forums.phpfreaks.com/topic/186191-user-login-with-activation-problem/#findComment-983489 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.