1

I'm new to learning php and in one of my first programs I wanted to make a basic php website with login capabilities with and array of the user and passwd.

my idea is to store the username as a list parameter and have the passwd as the contents, like this:

arr = array(username => passwd, user => passwd);

now my problem is that I don't know how I can read from the file (data.txt) so I can add it into the array.

data.txt sample:
username passwd
anotherUSer passwd

I've opened the file with fopen and stored it in $data.

Alvar
  • 252
  • 1
  • 4
  • 13
  • 3
    You shouldn't store sensitive information as plaintext. – Norse Jun 17 '12 at 21:28
  • On top of what @Norse advised, this can be easily googled. `file_get_contents` and `split` will do it for you. – Qix - MONICA WAS MISTREATED Jun 17 '12 at 21:29
  • @Norse I know I shouldn't store any sensitive information in plain text, but this is my first real php program and it's just for learning I don't see the point to make it too complicated. I will of course add an encryption if this will be a real website some day. – Alvar Jun 17 '12 at 21:41

5 Answers5

18

You can use the file() function.

foreach(file("data.txt") as $line) {
    // do stuff here
}
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • this seems like the simplest solution and the one I should use for this simple exercise. Thanks! :) – Alvar Jun 17 '12 at 22:28
6

Modify this PHP example (taken from the official PHP site... always check first!):

$handle = @fopen("/path/to/yourfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        echo $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

to:

$lines = array();
$handle = @fopen("/path/to/yourfile.txt", "r");
if ($handle) {
    while (($buffer = fgets($handle, 4096)) !== false) {
        $lines[] = $buffer;
    }
    if (!feof($handle)) {
        echo "Error: unexpected fgets() fail\n";
    }
    fclose($handle);
}

// add code to loop through $lines array and do the math...

Be aware that you should not store login details in a textfile that in addition is not encrypted, this approach has severe security issues. I know you are new from PHP, but the best approach is to store it in a DB and crypting the passwords with an algorithm such as MD5 or SHA1,

Cranio
  • 9,647
  • 4
  • 35
  • 55
  • of course it's best to encrypt passwords but since this is just for learning and I will not use this for any **REAL** website it's not a problem. – Alvar Jun 17 '12 at 21:47
  • I never understood that example it seemed too complicated to do a such a small operation. – Alvar Jun 17 '12 at 21:56
  • @Alvar It is indeed a quite common approach in many languages. You open a file, get a *handle* to it (a pointer, more or less), and read and seek through it by gradually advancing. The advantage, as another user has pointed out, is that you can handle very large files. For you the answer of the user who used `file()` can be enough. – Cranio Jun 17 '12 at 21:59
  • this answer is too complicated for this simple exercise. But it's a good one. :) – Alvar Jun 17 '12 at 22:29
  • Perfect answer, but lines[] need a $. – Dietrich Baumgarten Jun 12 '23 at 08:24
3

You shouldn't store sensitive information as plaintext, but to answer your question,

$txt_file = file_get_contents('data.txt'); //Get the file
$rows = explode("\n", $txt_file); //Split the file by each line

foreach ($rows as $row) {
   $users = explode(" ", $row); //Split the line by a space, which is the seperator between username and password
   $username = $users[0];
   $password = $users[1];
}

Take a look at this thread.

Community
  • 1
  • 1
Norse
  • 5,674
  • 16
  • 50
  • 86
  • Using `file_get_contents` and `explode` preserves empty lines (including the empty line at the end of the file, which git likes the most) as opposed to `file` (even setting the `FILE_IGNORE_NEW_LINES` flag won't help). – ozanmuyes Feb 04 '23 at 00:47
0

This works for extremely large files as well:

$handle = @fopen("data.txt", "r");
if ($handle) {
    while (!feof($handle)) { 
        $line = stream_get_line($handle, 1000000, "\n"); 
        //Do Stuff Here.
    } 
fclose($handle);
}
0

Use file() or file_get_contents() to create either an array or a string.

process the file contents as needed

// Put everything in the file in an array
$aArray = file('file.txt', FILE_IGNORE_NEW_LINES);

// Iterate throug the array
foreach ($aArray as $sLine) {

    // split username an password
    $aData = explode(" ", $sLine);

    // Do something with the username and password
    $sName = $aData[0];
    $sPass = $aData[1];
}