What are the advantages and disadvantages of storing user credentials in a text file versus a database for a PHP login system?

Storing user credentials in a text file can be simpler and easier to implement for a small-scale PHP login system. However, it is less secure as text files can be easily accessed and manipulated. On the other hand, storing user credentials in a database provides better security measures such as encryption and access control, but it requires more setup and maintenance.

// Example of storing user credentials in a text file
$file = 'users.txt';
$username = 'john_doe';
$password = 'password123';

$data = "$username:$password\n";
file_put_contents($file, $data, FILE_APPEND);

// Example of storing user credentials in a database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

$conn = new mysqli($servername, $username, $password, $dbname);

$username = 'john_doe';
$password = 'password123';

$sql = "INSERT INTO users (username, password) VALUES ('$username', '$password')";
$conn->query($sql);
$conn->close();