In what ways can PHP developers improve data security and privacy by transitioning from storing data in plaintext files to utilizing databases for data storage and retrieval in web applications?
Storing data in plaintext files can lead to security vulnerabilities as the data is easily accessible if the file is compromised. Transitioning to databases for data storage and retrieval in web applications can improve data security and privacy by allowing for encryption, user authentication, and access control.
// Connect to a MySQL database
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
$conn = new mysqli($servername, $username, $password, $dbname);
// Insert data into the database
$sql = "INSERT INTO users (username, password) VALUES ('john_doe', 'hashed_password')";
$conn->query($sql);
// Retrieve data from the database
$sql = "SELECT * FROM users WHERE username='john_doe'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo "Username: " . $row["username"] . " - Password: " . $row["password"];
}
} else {
echo "0 results";
}
$conn->close();
Related questions
-
Web scraping PHP cURL DOMDocument
-
PHP coding conventions syntax rules
-
SQL injection XSS attacks data validation