0

So I have simple registration form that write user and password in file.txt. When 1 line in the file has already user and password every next registration is on same line and in the same time put empty next line.

If the file is empty is writing properly, every registration is on new line. Here is the code that i use:

        if(!$error) {
        $input = $username . '|' . $pass ."\n";
        file_put_contents('users/file.txt', $input, FILE_APPEND);

        mkdir('users/'. $username);

        header('Location: index.php');
        exit;
    }

p.s. I'm sorry for my English.

Goro
  • 499
  • 1
  • 13
  • 31

2 Answers2

1

try it like this

if(!$error) {  
$input = $username . '|' . $pass ."\n";  
$fh = fopen("users/file.txt", 'a') or die("can't open file");  
fwrite($fh, $input);  
fclose($fh);  

header('Location: index.php');  
exit;  
}    
Erez83
  • 9
  • 1
  • If there is no empty second line in file is writing on the same line. This happen only if i have already one line and no second empty after first. – Goro Sep 27 '13 at 07:36
  • I guess I will save the file with empty second line. I can't find other solution. – Goro Sep 27 '13 at 07:38
1

perhaps this will be good for You:

if(!$error) {
    if (strlen(file_get_contents('users/file.txt')) > 1){
        $input = "\n" . $username . '|' . $pass;
    } else {
        $input = $username . '|' . $pass;
    }

        file_put_contents('users/file.txt', $input, FILE_APPEND);

        mkdir('users/'. $username);

        header('Location: index.php');
        exit;
    }
Adam
  • 1,371
  • 2
  • 11
  • 12