0

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.

6
  • 3
    Seriously? User credentials in a plain txt file? Have you ever thought about using any databases like mysql which would perfectly match to php? Commented Apr 17, 2019 at 13:11
  • 3
    It sounds like a homework or other learning problem @B001ᛦ, be nice. Commented Apr 17, 2019 at 13:12
  • The requirement was to use a plain text file ;( Commented Apr 17, 2019 at 13:13
  • First issue I see is instantiating the array of credentials, but not wrapping each one in single or double quotes: $Array = array(Dave,Password,Lucy,Password2,Chris,Password3); Commented Apr 17, 2019 at 13:13
  • be nice... Is there anything that is not "nice" in my comment? If so then flag comment and move on please @Ewan Commented Apr 17, 2019 at 13:13

1 Answer 1

1

Your problem is that file is giving you values with newline characters attached to them. From the manual:

Note:
Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used.

To work around this, you can either include the FILE_IGNORE_NEW_LINES option in your call i.e.

$CollectedDetails = file('UsernamesandPasswords.txt', FILE_IGNORE_NEW_LINES);

or you can trim the values before comparing them i.e.

$Array[] = trim($EachValue);

Using trim may be safer in case there is other unexpected whitespace in the file.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.