-1

I want to create a registration system on my site where only limited users will be able to create their account. I want to use a .txt file for storing usernames and passwords.

I have the following code so far :

$uname=$_POST['usr'];
$pass=$_POST['pwd'];

if(empty($_POST["ok"])){echo "Could not insert data!";}
 else
{$file=fopen("user.txt","w");
echo fwrite($file,$uname);
fclose($file);}

This receives the user data from a form and puts it in user.txt file.

My problem is that when new data is inserted to txt file the old data get deleted.

I want to keep the data in txt file like

  foo:12345~bar:1111

username and password are seprated by : and new user is seprated by ~ ,later I will use regex to get the data from txt file.

How can i correct my code to keep both new and old data?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • 3
    You need to open file in append mode http://php.net/manual/en/function.fopen.php – Saty Nov 28 '15 at 13:35
  • 1
    Possible duplicate of [php create or write/append in text file](http://stackoverflow.com/questions/24972424/php-create-or-write-append-in-text-file) – Aerendir Nov 28 '15 at 13:39
  • 3
    **plain text stored password are evil** use some [hashing function](http://php.net/manual/en/faq.passwords.php); custom storage standard too, **use CSV with [fputscv](http://php.net/manual/fr/function.fputcsv.php)** ; file storage custom engine is **really weak on concurrent write** use sqlite or other atomic tool – Blag Nov 28 '15 at 13:42

5 Answers5

2

You need to open file in append mode

http://php.net/manual/en/function.fopen.php

<?php

$uname = $_POST['usr'];
$pass = $_POST['pwd'];

if (empty($_POST["ok"])) {
    echo "Could not insert data!";
} else {
    $file = fopen("user.txt", "a");
    $srt="foo:".$uname."~bar:".$pass;// create your string 
    echo fwrite($file, $srt);
    fclose($file);
}
Saty
  • 22,443
  • 7
  • 33
  • 51
  • 1
    Thank you @saty , you have solved a part of my proble, now old data is not getting deleted. but it put the data as **foo:user~bar:1234** i want to update the foo column with $uname, and 1234 with $pwd ,~bar:1234 will be for next user registration. – Amit Verma Nov 28 '15 at 14:06
1

If we want to add on to a file we need to open it up in append mode.

So you need to change from write only mode to append mode.

$file=fopen("user.txt","a");
Manikiran
  • 2,618
  • 1
  • 23
  • 39
1

To answer your question: you have to explicitly pass $mode argument to fopen() function equals to 'a'.

However, it looks like a bad idea to use plain files for this task. Mainly because of concurent writes troubles.

Ivan Velichko
  • 6,348
  • 6
  • 44
  • 90
1

This is really a bad choice: there are a lot of drawbacks for security, for read/write times, for concurrent requests and a lot more. Using a database isn't difficult, so my suggestion is to use one.

Anyway, your question is asked yet here: php create or write/append in text file

Community
  • 1
  • 1
Aerendir
  • 6,152
  • 9
  • 55
  • 108
1

Simple way to append to a file:

file_put_contents("C:/file.txt", "this is a text line" . PHP_EOL, FILE_APPEND | LOCK_EX);
Buffalo
  • 3,861
  • 8
  • 44
  • 69