1

I am trying to break the line after this code. I can't figure it out what ever I do the output of my code is just as shown below:

Output:

User: adminLogged in: 2014-02-09 05:34:30User: adminLogged OUT: 2014-02-09 05:34:36User: tataLogged in: 2014-02-09 05:34:41User: tataLogged OUT: 2014-02-09 05:34:43

I want to set some space and new line.

$date=date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";  
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username";
fwrite($fh, "$stringData");
$stringData = "Logged in: $date";
fwrite($fh, "$stringData");
fclose($fh);
Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
Mrtata01
  • 23
  • 2
  • 7
  • 1
    Please read this...http://stackoverflow.com/questions/3066421/writing-a-new-line-to-file-in-php – VIVEK-MDU Feb 09 '14 at 05:39
  • Add "/n" to your lines. It's called a newline. Unless your environment is a browser, not a terminal - then you could use
    – JAL Feb 09 '14 at 05:40
  • `fwrite($fh, "$stringData\n");`. or better yet: `fwrite($fh, "$stringData".PHP_EOL);` – Wrikken Feb 09 '14 at 05:40

2 Answers2

1

Just make use of \n or \r in front of your fwrite().

Like this.

fwrite($fh, "\n$stringData");
              ^-------- // Add this to both of your fwrite() calls
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

Try this

$date = date("Y-m-d H:i:s");
$updatefile = "userlogs.txt";  
$fh = fopen($updatefile, 'a') or die("can't open file");
$stringData = "User: $username";
fwrite($fh, "$stringData\n");
$stringData = "Logged in: $date";
fwrite($fh, "$stringData\n");
fclose($fh);

Here you just add \n before or after variable assign just like this fwrite($fh, "$stringData\n"); or fwrite($fh, "\n$stringData");.

For more details, see this question.

Community
  • 1
  • 1
VIVEK-MDU
  • 2,483
  • 3
  • 36
  • 63