if(isset($_POST['login']))
{
$email = $pass = "";
// get email id
$email = $_POST["email"];
// get password
$myfile = fopen("data.txt", "w") or die("Unable to open file!");
$pass = $_POST["pass"];
$txt = "$email:$pass\n";
fwrite($myfile, $txt);
fclose($myfile);
}
He doesn't create a new line in data.txt and he replace the first line
CodePudding user response:
Open the file in append mode:
fopen("data.txt", "a")
CodePudding user response:
The problem is in your file mode usage fopen("data.txt","xxxx")
so check this table,
Mode | Description |
---|---|
"r" |
Opens a file for reading. The file must exist. |
"w" |
Creates an empty file for writing. If a file with the same name already exists, its content is erased and the file is considered as a new empty file. |
"a" |
Appends to a file. Writing operations, append data at the end of the file. The file is created if it does not exist. |
"r " |
Opens a file to update both reading and writing. The file must exist. |
"w " |
Creates an empty file for both reading and writing. |
"a " |
Opens a file for reading and appending. |
"rb" |
Opens a binary file for reading. The file must exist. |
"wb" |
Creates an empty binary file for writing. If the file exists, its contents are cleared unless it is a logical file. |
"ab" |
Opens a binary file in append mode for writing at the end of the file. The fopen function creates the file if it does not exist. |
"rb " |
Opens a binary file for both reading and writing. The file must exist. |
"wb " |
Creates an empty binary file for both reading and writing. If the file exists, its contents will be cleared unless it is a logical file. |
"ab " |
Opens a binary file in append mode for writing at the end of the file. The fopen() function creates the file if it does not exist. |
For solving your problem you can use appending modes a
, a
, ab
, ab
.
Change your code like this:
if(isset($_POST['login']))
{
$email = $pass = "";
// get email id
$email = $_POST["email"];
// get password
$myfile = fopen("data.txt", "a") or die("Unable to open file!");
$pass = $_POST["pass"];
$txt = "$email:$pass\n";
fwrite($myfile, $txt);
fclose($myfile);
}