PHP 写入新行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15130289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP fwrite new line
提问by Dynamiite
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
我正在尝试将用户名和密码写入 txt 文件中的新行。txt 文件中的输出应该是这样的。我知道这不是很安全,但仅用于学习目的
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);
}
?>
回答by Dino
回答by VolkerK
You append a newline to both the username andthe 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';
使用fwrite($fh,$user." ".$password."\n");,而不是让他们都在同一行。
或者使用fputcsv()写入数据并fgetcsv()获取它。这样你至少可以避免编码问题,例如$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) ;-)
...即抛开将普通密码存储在普通文件中并使用 _GET 进行此类操作(使用 _POST 代替)的所有错误;-)
回答by Thrallix
fwrite($handle, "<br>"."\r\n");
Add this under
在下面添加这个
$password=$_POST['password'].PHP_EOL;
this. .
这个。.
回答by Mark Adewale
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.
然后你就可以list($username,$password) = explode(':',file_get_contents('users.txt'));用来解析你端的数据了。

