What are common pitfalls to avoid when using meta refresh in PHP?
Common pitfalls to avoid when using meta refresh in PHP include not setting a proper delay time, not providing a valid URL to redirect to, and not handling potential security risks such as open redirects. To solve these issues, make sure to specify a delay time in seconds, validate the URL being redirected to, and sanitize user input to prevent open redirects.
<?php
$delay = 5; // Set delay time in seconds
$url = "https://example.com"; // Specify the valid URL to redirect to
// Validate the URL
if (filter_var($url, FILTER_VALIDATE_URL)) {
// Sanitize user input to prevent open redirects
header("Refresh: $delay; URL=$url");
exit();
} else {
echo "Invalid URL";
}
?>