How can PHP developers ensure the security of their code, especially when dealing with user input like usernames and passwords?
To ensure the security of their code when dealing with user input like usernames and passwords, PHP developers should always sanitize and validate user input to prevent SQL injection and cross-site scripting attacks. They should also use secure hashing algorithms like bcrypt to store passwords securely.
// Sanitize and validate user input
$username = filter_var($_POST['username'], FILTER_SANITIZE_STRING);
$password = filter_var($_POST['password'], FILTER_SANITIZE_STRING);
// Hash the password using bcrypt
$hashed_password = password_hash($password, PASSWORD_DEFAULT);