What are some common pitfalls to avoid when using PHP to redirect users based on their URL?

One common pitfall to avoid when using PHP to redirect users based on their URL is not properly sanitizing user input, which can lead to security vulnerabilities such as injection attacks. To prevent this, always validate and sanitize user input before using it in a redirect function. Additionally, make sure to use a safe method of redirecting users, such as using header() function with exit() to prevent further script execution.

// Validate and sanitize user input
$url = filter_var($_GET['url'], FILTER_SANITIZE_URL);

// Redirect user to the sanitized URL
if (filter_var($url, FILTER_VALIDATE_URL)) {
    header("Location: $url");
    exit();
} else {
    echo "Invalid URL";
}