What are best practices for handling special characters, like accents and symbols, in PHP forms to avoid errors related to session start and header modification?

Special characters, like accents and symbols, can cause issues with session start and header modification in PHP forms. To avoid errors, it's recommended to use UTF-8 encoding and htmlspecialchars function to sanitize user input before processing it.

// Start the session with UTF-8 encoding
session_start();
header('Content-Type: text/html; charset=utf-8');

// Sanitize user input using htmlspecialchars
$user_input = htmlspecialchars($_POST['user_input'], ENT_QUOTES, 'UTF-8');