Jump to content

If-Else Statement


Xtremer360

Recommended Posts

I'm trying to figure out what is wrong with my else statement because I am purposely putting in the wrong password for testing of my if-else statements and its not giving me the wrong error message. It should be saying the chances left message but instead its saying the "Invalid Username and Password combination!"

 

else {
                            
                        $numberOfAttempts = $_SESSION['numberOfAttempts']+1;
                        
                        if ($numberOfAttempts < 5) {
                            
                            $chancesLeft = 5 - $numberOfAttempts;
                            
                            $output = array('errorsExist' => true, 'message' => 'You have' .$chancesLeft.' to login succesfully or the account will be locked!'); 
                            
                        } else {
                            
                            
                            $output = array('errorsExist' => true, 'message' => 'Your account is currently locked, we appologize for the inconvienence. This is a security messure implimented by to many failed login\'s!');
                            
                        }
                        
                        $output = array('errorsExist' => true, 'message' => 'Invalid Username and Password combination!');
                            
                    }

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/
Share on other sites

1. do you have session_start() at the top of your page

 

2. Have you tried to echo your session to confirm that the correct value is being stored.

 

3. I would set up my code as so

else {
                            
                        $numberOfAttempts = $_SESSION['numberOfAttempts']+1;
                        
                        if ($numberOfAttempts < 5) {
                            
                            $chancesLeft = 5 - $numberOfAttempts;
                            
                            $output = array('errorsExist' => true, 'message' => 'You have' .$chancesLeft.' to login succesfully or the account will be locked!'); 
                            
                        } else if($numberOfAttempts >= 5) {
                            
                            
                            $output = array('errorsExist' => true, 'message' => 'Your account is currently locked, we appologize for the inconvienence. This is a security messure implimented by to many failed login\'s!');
                            
                        } else {
                        
                        $output = array('errorsExist' => true, 'message' => 'Invalid Username and Password combination!');
                            
                        }
                    }

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/#findComment-1228437
Share on other sites

because after your script is finished executing, your $_SESSION['numberofattempts'] value remains the same value, which I am assuming is 0, because you have not changed the value of the session, you simply stored it into a variable and added 1 it externally. insed of you else statement you will need to redefine the value of you session, e.g

$_SESSION['numberOfAttempts'] = $_SESSION['numberOfAttempts']+1

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/#findComment-1228452
Share on other sites

I'm a little confused are you saying this:

 

else {
                        $_SESSION['numberOfAttempts'] = $_SESSION['numberOfAttempts']+1;    
                        $numberOfAttempts = $_SESSION['numberOfAttempts']+1;
                        
                        if ($numberOfAttempts < 5) {
                            
                            $chancesLeft = 5 - $numberOfAttempts;
                            
                            $output = array('errorsExist' => true, 'message' => 'Invalid Username and Password combination! You have ' .$chancesLeft.' to login succesfully or the account will be locked!'); 
                            
                        } else {
                            
                            
                            $output = array('errorsExist' => true, 'message' => 'Your account is currently locked, we appologize for the inconvienence. This is a security messure implimented by to many failed login\'s!');
                            
                        }
                            
                    }

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/#findComment-1228457
Share on other sites

almost.

else {
                        $_SESSION['numberOfAttempts'] = $_SESSION['numberOfAttempts']+1;    
                        $numberOfAttempts = $_SESSION['numberOfAttempts']; //you already added one here
                        
                        if ($numberOfAttempts < 5) {
                            
                            $chancesLeft = 5 - $numberOfAttempts;
                            
                            $output = array('errorsExist' => true, 'message' => 'Invalid Username and Password combination! You have ' .$chancesLeft.' to login succesfully or the account will be locked!'); 
                            
                        } else {
                            
                            
                            $output = array('errorsExist' => true, 'message' => 'Your account is currently locked, we appologize for the inconvienence. This is a security messure implimented by to many failed login\'s!');
                            
                        }
                            
                    }

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/#findComment-1228458
Share on other sites

I have one more thing to add onto that though. I'm trying to figure out some logic here if the user gets an account locked then should I keep updating the lockDate column each time and put in a new row into the hacking table of each time an account has gone pasted the 4 chances or how should I approach this?

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/#findComment-1228467
Share on other sites

Forgot to update my code.

 

else {
                            
                        $_SESSION['numberOfAttempts'] = $_SESSION['numberOfAttempts']+1;
                        $numberOfAttempts = $_SESSION['numberOfAttempts'];
                        
                        if ($numberOfAttempts < 5) {
                            
                            $chancesLeft = 5 - $numberOfAttempts;
                            
                            $output = array('errorsExist' => true, 'message' => 'Invalid Username and Password combination! You have ' .$chancesLeft.' to login succesfully or the account will be locked!'); 
                            
                        } else {
                            
                            $lockDate = date('Y-m-d H:i:s', time());
                            $hackerIPAddress = $_SERVER['REMOTE_ADDR'];
                            $userID = $row['userID'];
                            $query = "UPDATE manager_users SET lockDate = '".$lockDate."' WHERE userID = '".$userID."'";
                            $result = mysqli_query($dbc,$query);
                            $query2 = "INSERT INTO manager_users_hacking (hackerIPAddress, userID, lockDate) VALUES ('".$hackerIPAddress."','".$userID."', '".$lockDate."')";
                            $result2 = mysqli_query($dbc,$query2);
                            $output = array('errorsExist' => true, 'message' => 'Your account is currently locked, we appologize for the inconvienence. This is a security messure implimented by to many failed login\'s!');
                            
                        }
                            
                    }

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/#findComment-1228470
Share on other sites

I have one more thing to add onto that though. I'm trying to figure out some logic here if the user gets an account locked then should I keep updating the lockDate column each time and put in a new row into the hacking table of each time an account has gone pasted the 4 chances or how should I approach this?

this is really up to you, however, if you are storing the date that a user becomes locked from their account, i would not continually update the date if they try to gain access again. I would simply make the user aware of the time that they can gain access into their account again. I also would not keep track of the amount of times that the user has gone over 4 times. You want to make your site as user friendly as possibly.

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/#findComment-1228473
Share on other sites

Dang I can't edit my post anyway what I want to do is:

 

NEW CODE: http://pastebin.com/vFZwmJuc

 

I'm trying to figure out what I need to do so that it can see if after the account gets locked if its at that 10 minute mark passed the lockDate if there is a lockDate other than the 0000-00-00 00:00:00 and then if is then turn the lockDate back to 0000-00-00 and resets the numberOfLogins.

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/#findComment-1228580
Share on other sites

Just wanted to inform everyone that I thought I'd declare a new variable of what the current dateTime is and compare it against the lockDate to see if there is a 10 minute difference and then if it is then Update the lockDate and update the numberOfAttempts variable.

 

I can't find a function that can compare two timedates. Anyone have one?

Link to comment
https://forums.phpfreaks.com/topic/239093-if-else-statement/#findComment-1228604
Share on other sites

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.