Are there any best practices for securely retrieving and displaying user data in PHP?
To securely retrieve and display user data in PHP, it is important to sanitize user input to prevent SQL injection attacks and cross-site scripting (XSS) attacks. One way to achieve this is by using prepared statements with parameterized queries to interact with the database. Additionally, data should be properly escaped before being displayed to prevent XSS attacks.
// Securely retrieve user data using prepared statements
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :username");
$stmt->bindParam(':username', $username);
$stmt->execute();
$userData = $stmt->fetch();
// Display user data after escaping
echo htmlspecialchars($userData['username']);