I'm playing around with a basic PHP site just for my own knowledge and I basically have a text file filled with "passwords". I've opened this text file as an array into my page, but when I search for a specific element, the only password that shows as valid is the last option, and it seems to ignore every other element in the array.
So if my password list is
passwords
12345
test
qwerty
When I search for the first 3 elements, it says that it does not exist in the array. However, if I searched for the last value, which is 'qwerty' here, it will match.
<?php
$file = explode("\n", file_get_contents("passwords.txt"));
//Call the function and pass in the list
if (isset($_POST['submit_login'])) {
//Set the search variable
$search = $_POST['password'];
search_array($file, $search);
} else {
//echo "You did not search! <br />";
//echo "<a href='searchForm.html'>Try again?</a>";
}
function search_array($array_value, $search_query)
{
echo "<span><h4>The Array List: </h4><span/>";
foreach ($array_value as $key => $value) {
echo $key." ";
echo $value."<br />";
}
if ($value == $search_query) {
echo "<h5>Search Stuff</h5>";
echo "You searched for: " . $search_query . "</br>";
echo "Your search was found in the index #" .$key. "<br />";
} else {
echo "You searched for: " . $search_query . "</br>";
echo "Your search did not match any of the items. <br />";
echo "<a href='searchForm.html'>Try again?</a>";
}
}
?>
However, if I searched for '12345' for example, which is index 1 in the array, I will get the output
You searched for: 12345
Your search did not match any of the items.
But, searching for 'qwerty' which is the last element yields the desired response.