I'm having problems when trying to use a for each statement to store values inside of an array. So the aim of my program is to store the content of a text file inside of an array and by doing so I'm able to check whether the user's input is the same as the content stored inside of the text file. The problem is that when comparing the values in the array to the user's input, the program isn't able to detect whether the user's input is the same as the content stored in the array. My verdict on the situation is that when using the for each statement to store the content of the text file into an array space is added between each value. (My verdict could be wrong).
Main Code:
<html>
<body>
<form name="LoginForm" method="post">
<label>Username</label>
<input type="text" name="Username"/>
<label>Password </label>
<input type="Password" name="Password" />
<input type="submit"/>
</form>
<?php
$Username = $_POST["Username"];
$Password = $_POST["Password"];
$CollectedDetails = file('UsernamesandPasswords.txt');
$Array = array();
foreach($CollectedDetails as $EachValue)
{
$Array[] = $EachValue;
}
for($Loop = 0; $Loop < 6; $Loop+=2)
{
if($Username == $Array[$Loop] && $Password == $Array[$Loop+1])
{
echo "Access Permitted";
}
else
{
echo "Access Denied";
}
}
?>
</body>
</html>
Content Stored inside of the text file Dave Password Lucy Password2 Chris Password3
Below is a working updated version of the program above without the addition of the text file. (I decided to add it in case it helps).
<html>
<body>
<form name="LoginForm" method="post">
<label>Username</label>
<input type="text" name="Username"/>
<label>Password </label>
<input type="Password" name="Password" />
<input type="submit"/>
</form>
<?php
$Username = $_POST["Username"];
$Password = $_POST["Password"];
$Array = array(Dave,Password,Lucy,Password2,Chris,Password3);
$Successful = false;
for($Loop = 0; $Loop < 6; $Loop+=2)
{
if($Username == $Array[$Loop] && $Password == $Array[$Loop+1])
{
echo "Access Permitted";
$Successful = true;
}
else if ($Successful == false)
{
echo "Access Denied";
}
}
?>
</body>
</html>
Note that both programs are supposed to produce the same outcome. Is there a possible solution to this issue, I can't seem to figure out the problem. Any Help would be appreciated.
$Array = array(Dave,Password,Lucy,Password2,Chris,Password3);