-1

I am working on a login service that accepts two fields ($username and $password) in login.html and passes them as post data to login.php. I am also to read a text document called 'pass.txt' into an array. The file looks like this.

anelhams0:7c4a8d09ca3762af61e59520943dc26494f8941b
jattenborough1:5baa61e4c9b93f3f0682250b6cf8331b7ee68fd8
nmalins2:7c222fb2927d828af22f592134e8932480637c0d
dlingley3:b1b3773a05c0ed0176787a4f1574ff0075f7521e

It continues for another 1800 or so lines. Each line of the txt file is a username:sha1password. I am to take the value for the username and check to see if it matches the username in the text file array. To do that I will need to instead separate the document by username and password only. so for example instead of array[0] being 'anelhams0:7c4a8d09ca3762af61e59520943dc26494f8941b' I need to separate it out to array[0] = 'anelhams0' and array[1] = '7c4a8d09ca3762af61e59520943dc26494f8941b' and array[2] = 'jattenborough1' and etc. I cannot find any way to do this because there are two separators I am using. Is there any way to accomplish this using some other function or a looping system? This is what I've got so far in login.php

<?php
//Takes our username and password
$username = $_POST["username"];
$password = $_POST["password"];


//read in our text file to an array (each element is a line from the text)
$fileSort = file('pass.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

ghoolie
  • 19
  • 3

1 Answers1

1

I have no idea why you called the array you read from the file $fileSort. I'll rename it to something that makes more sense to me: $allCredentials. All you need to do is loop through the array and split the line on the colon.

$givenUsername = $_POST["username"];
$givenPassword = $_POST["password"];

$allCredentials = file('pass.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach ($allCredentials as $credentials) {
    // version 1: access credentials directly
    list($username, $password) = explode(':', $credentials);
    // version 2: or store them in a new array
    $newCredentials[] = explode(':', $credentials);
}

// here's a way to output the new array
echo '<pre>';
print_r($newCredentials);
echo '</pre>';

See: foreach, list() and explode().

KIKO Software
  • 15,283
  • 3
  • 18
  • 33
  • How do I get a variable out of this? What variable would I compare it to? When I printed this out each element is still the same as it was, a username:sha1password. I need username as one element and sha1password as another for each line of the aray. – ghoolie Nov 12 '20 at 23:14
  • @cupOfCoding I changed my answer slightly. There are now two versions you could use. Don't use them both. It would make most sense to use version one, and get the check over with there and then, instead of first creating a new array and then check against that. – KIKO Software Nov 12 '20 at 23:25
  • Thank you, my question is: "How do I compare your version one?" Like how do I check to see if the value that was entered matches one of the elements in $allCredentials? This looping and methodology is new to me because I am in school for this right now. – ghoolie Nov 12 '20 at 23:32
  • I understand this is new to you, that's what the three links to the manual are for. Read the manual carefully, and above all experiment, try to understand these three things. When you do, you will probably be ready for the comparison itself. I'm sorry, I can't make learning to program any easier than that. You have to invest time and try to completely understand what the code does. – KIKO Software Nov 12 '20 at 23:38
  • For some reason I cant even print $newCredentials or their elements without an error. Can you give any advice? – ghoolie Nov 12 '20 at 23:39
  • @cupOfCoding I added a few lines that will output the new array. – KIKO Software Nov 12 '20 at 23:40