+ 1

Break Issue... Logic Error

So there's obviously a logic error somewhere, as the Do While Loop isn't breaking... Help me please: <?php /* Program: KENNEM_Task 5_Log In. AIM: To create a functional logging in system. Author: Maggie Kennedy Date: 30/08/2017 - 00/00/2017 **********************************************************************/ /*Create arrays with 5 IDs and 5 Password respectively.*/ $ID = array ('George', 'Peter', 'Wendy', 'Sheri', 'DummyID'); //IDs $Pass = array ('GeoNumber1', 'ImissTinkerBell', 'MyKiss', 'Tryme', 'DummyPW'); //Passwords /*Logging In Input.*/ $Check_ID = "Sher"; $Check_Pass = "Tryme"; /*Create a variable to trigger when loggin was successful. Also, create the Position variable*/ $Success = false; $Pos = 0; /*Start a Do While Loop to check whether ID and Pass match.*/ do { if ($Check_ID == $ID[$Pos]) { /*If ID is found in array then check that the password matches*/ if ($Check_Pass == $Pass[$Pos]) { $Success = true; /*Change $Success to true*/ break; } //End of Second If Statement } //End of First If Statement $Pos++; if ($Success == false && $Check_ID == 'DummyID') { break; } //End of Third If Statement } /*End of Do Loop*/ while ($Check_ID != 'DummyID' && $Success == false); /*While there are still IDs left or whenever $Success is still false.*/ if ($Success == true) /*If logging is successful*/ echo "<h2><center>Excellent! you have logged in as: $Check_ID!</center></h2>"; if ($Success == false) echo "<h2><center>Sorry, something went wrong. Make sure you typed in the right ID and Password.</center></h2>"; ?>

31st Aug 2017, 1:47 AM
Maggie KENNEDY
Maggie KENNEDY - avatar
1 Antwort
+ 2
There are a lot of issues with the code, but lets walk through what I see one at a time. if ($Check_ID == $ID[$Pos]) { /*If ID is found in array then check that the password matches*/ if ($Check_Pass == $Pass[$Pos]) { Your $Check_ID and $Check_Pass values are hard coded above: $Check_ID = "Sher"; $Check_Pass = "Tryme"; But you're starting at the beginning of the array, position 0, which would be: George / GeoNumber1. Rather than hard coding your position and the Check_ID / Check_Pass values, use a loop counter and reference the elements that way. I don't have a way to execute and step through your code right now, but that's the major flaw I see.
31st Aug 2017, 6:50 PM
vvvvv
vvvvv - avatar