Skip to content
PHP Logo

What are the best practices for handling redirects in PHP to open in a new tab or window?

When handling redirects in PHP to open in a new tab or window, you can achieve this by using JavaScript to open the redirect URL in a new tab or window. This can be done by adding a window.open() function call in the header of the PHP script that performs the redirect.

php
<?php
// Perform the redirect
header('Location: https://www.example.com');

// Use JavaScript to open the redirect URL in a new tab
echo '<script>window.open("https://www.example.com", "_blank");</script>';
exit;
?>