PHP fwrite new line

后端 未结 4 970
遇见更好的自我
遇见更好的自我 2020-12-03 04:36

I\'m trying to write username and password to a new line in a txt file. The output should be something like this in the txt file. I know this is not very secure but its just

相关标签:
4条回答
  • 2020-12-03 04:46

    How about you store it like this? Maybe in username:password format, so

    sebastion:password123
    anotheruser:password321
    

    Then you can use list($username,$password) = explode(':',file_get_contents('users.txt')); to parse the data on your end.

    0 讨论(0)
  • 2020-12-03 04:52

    Use PHP_EOL which produces \r\n or \n

    $data = 'my data' . PHP_EOL . 'my data';
    $fp = fopen('my_file', 'a');
    fwrite($fp, $data);
    fclose($fp);
    

    // File output

    my data
    my data
    
    0 讨论(0)
  • 2020-12-03 04:52

    You append a newline to both the username and the password, i.e. the output would be something like

    Sebastian
    password
    John
    hfsjaijn
    

    use fwrite($fh,$user." ".$password."\n"); instead to have them both on one line.
    Or use fputcsv() to write the data and fgetcsv() to fetch it. This way you would at least avoid encoding problems like e.g. with $username='Charles, III';

    ...i.e. setting aside all the things that are wrong about storing plain passwords in plain files and using _GET for this type of operation (use _POST instead) ;-)

    0 讨论(0)
  • 2020-12-03 04:59
    fwrite($handle, "<br>"."\r\n");
    

    Add this under

    $password = $_POST['password'].PHP_EOL;
    

    this. .

    0 讨论(0)
提交回复
热议问题