Php fwrite append new line

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 for learning purposes

Sebastian   password
John        hfsjaijn

This is what i have so far

if(isset($_GET['register'])) //  
{
    $user  = $_GET['username'];
    $password=$_GET['password'];
    $fh = fopen("file.txt","a+");
    fwrite($fh,$user."\n"); //write to txtfile
    fwrite($fh,$password."\n"); // write to txtfile
    fclose($fh);
}

EDIT: Here's the solution for me:

if (isset($_POST['register'])) {
   $user  = $_POST['username'];
   $password = $_POST['password'].PHP_EOL;
   $fh = fopen("file.txt","a+");
   fwrite($fh,$user." ".$password); //write to txtfile
  
   fclose($fh);
}

Php fwrite append new line

Martijn

15.5k4 gold badges36 silver badges66 bronze badges

asked Feb 28, 2013 at 7:56

6

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

Php fwrite append new line

candlejack

1,1692 gold badges21 silver badges50 bronze badges

answered Feb 28, 2013 at 8:03

Php fwrite append new line

Dino BabuDino Babu

5,7663 gold badges23 silver badges33 bronze badges

1

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) ;-)

answered Feb 28, 2013 at 8:01

VolkerKVolkerK

94.3k20 gold badges162 silver badges225 bronze badges

2

fwrite($handle, "
"."\r\n");

Add this under

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

this. .

Php fwrite append new line

answered Dec 2, 2014 at 20:53

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.

answered Jan 26, 2019 at 17:05

Php fwrite append new line

Php fwrite append new line

    • Share

Hi

I have this code:

$Address will store IP addresses. How can I get each new IP to go in it's own line?  Is there a way of having a number go before it aswell like this:

1.  xxx.xxx.xxx.xxx

2  xxx.xxx.xxx.xxx

  • Quote

Link to comment
Share on other sites

Php fwrite append new line
Php fwrite append new line

Php fwrite append new line

  • Author

    • Share

I tried that but every time I loaded the site it would just display a white screen.  How exactly should I type it?

  • Quote

Link to comment
Share on other sites

Php fwrite append new line
Php fwrite append new line

    • Share

please paste the exact changes in the code so we can see how it is being used what is causing the error with /n 

Php fwrite append new line

  • Quote

Link to comment
Share on other sites

Php fwrite append new line

  • Author

    • Share

For some reason the backslashes are not displaying but they are there.

  • Quote

Link to comment
Share on other sites

Php fwrite append new line
Php fwrite append new line

    • Share

it needs to be

  • Quote

Link to comment
Share on other sites

Php fwrite append new line

  • Author

    • Share

Hi

Just tried that but now nothing gets written to the txt file.  Any ideas?

  • Quote

Link to comment
Share on other sites

Php fwrite append new line
Php fwrite append new line

    • Share

  • Quote

Link to comment
Share on other sites

Php fwrite append new line
Php fwrite append new line

    • Share

Blade beat me to it oh well this will work too

Php fwrite append new line

  • Quote

Link to comment
Share on other sites

Php fwrite append new line

  • Author

    • Share

Hi

Thanks, that's it working now.  Do you know how to insert a space?

  • Quote

Link to comment
Share on other sites

Php fwrite append new line
Php fwrite append new line

    • Share

Hello Rose,

I have tested your code and is working fine here. I have changed the permission of the file (addresses.txt).. Thats all..

check this code..

Regards,

Joseph..

  • Quote

Link to comment
Share on other sites

Php fwrite append new line
Php fwrite append new line

    • Share

by pressing the space key, you have to do it in the variable it's self or do you mean

  • Quote

Link to comment
Share on other sites

Php fwrite append new line

  • Author

    • Share

yes, that's it.

Thank you for all your help.

  • Quote

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Does Fwrite add new line?

When writing to a file in PHP with fwrite , you can add a newline character with PHP_EOL. Using PHP_EOL instead of manually writing out a "\n" or "\r\n" is important to make your code as portable as possible.

How do I add a new line in PHP?

Answer: Use the Newline Characters ' \n ' or ' \r\n ' You can use the PHP newline characters \n or \r\n to create a new line inside the source code. However, if you want the line breaks to be visible in the browser too, you can use the PHP nl2br() function which inserts HTML line breaks before all newlines in a string.

What does Fwrite return in PHP?

fwrite() function in PHP The fwrite() function writes to an open file. It returns the number of bytes written on success, whereas returns False on failure.