What potential pitfalls should be considered when using the target attribute in PHP to open a new page?

When using the target attribute in PHP to open a new page, one potential pitfall to consider is the possibility of the user being redirected to a malicious website if the target attribute value is not properly sanitized. To avoid this, always validate and sanitize user input before using it in the target attribute.

<?php
// Sanitize the target attribute value before using it
$target = filter_var($_POST['target'], FILTER_SANITIZE_URL);

// Use the sanitized target attribute value in the link
echo '<a href="example.php" target="' . $target . '">Click me</a>';
?>