How can a PHP login script be made more secure when handling important data?
To make a PHP login script more secure when handling important data, it is crucial to use prepared statements to prevent SQL injection attacks, properly sanitize user input to prevent cross-site scripting attacks, and use secure password hashing techniques like bcrypt to store passwords securely.
// Example of using prepared statements to prevent SQL injection
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username AND password = :password');
$stmt->execute([
'username' => $username,
'password' => $password
]);
// Example of sanitizing user input to prevent cross-site scripting
$username = htmlspecialchars($_POST['username']);
$password = htmlspecialchars($_POST['password']);
// Example of using bcrypt for secure password hashing
$options = ['cost' => 12];
$hashedPassword = password_hash($password, PASSWORD_BCRYPT, $options);