What are some best practices for passing message IDs via URL in PHP?

When passing message IDs via URL in PHP, it is important to sanitize and validate the input to prevent security vulnerabilities such as SQL injection or cross-site scripting attacks. One best practice is to use PHP's filter_input() function with the FILTER_VALIDATE_INT filter to ensure that the ID is an integer value. Additionally, consider using prepared statements when querying the database to further protect against SQL injection.

// Sanitize and validate the message ID passed via URL
$message_id = filter_input(INPUT_GET, 'message_id', FILTER_VALIDATE_INT);

if($message_id === false) {
    // Handle invalid message ID
    echo "Invalid message ID";
} else {
    // Use prepared statements to query the database with the sanitized message ID
    $stmt = $pdo->prepare("SELECT * FROM messages WHERE id = :message_id");
    $stmt->bindParam(':message_id', $message_id, PDO::PARAM_INT);
    $stmt->execute();

    // Fetch and process the message data
    $message = $stmt->fetch(PDO::FETCH_ASSOC);
}