What are the best practices for handling page redirection within an iframe using PHP?
When handling page redirection within an iframe using PHP, it is important to ensure that the redirection works smoothly within the iframe without affecting the parent page. One common approach is to use JavaScript to handle the redirection within the iframe itself.
<?php
// Check if the request is coming from within an iframe
if(isset($_SERVER['HTTP_REFERER']) && strpos($_SERVER['HTTP_REFERER'], 'example.com') !== false) {
echo '<script>window.top.location.href = "new_page.php";</script>';
exit;
} else {
// Handle redirection for requests not coming from within an iframe
header('Location: new_page.php');
exit;
}
?>
Keywords
Related Questions
- What are the best practices for structuring PHP code to handle multiple database connections on the same page?
- What potential pitfalls should be avoided when using the strtotime() function in PHP for date calculations?
- How can the date() function be used to display the current time accurately in PHP?