PHP fwrite new line

匿名 (未验证) 提交于 2019-12-03 02:14:01

问题:

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

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

回答1:

Use PHP_EOL which produces \r\n or \n

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

// File output

my data my data


回答2:

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



回答3:

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

Add this under

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

this. .



转载请标明出处:PHP fwrite new line
文章来源: PHP fwrite new line
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!