| In PHP, you can implement a 302 Found redirection using the header() function to send the appropriate HTTP headers. Here's an example of how you can use it: <?php
// Set the new location URL
$newLocation = 'https://example.com/new-page.php';
// Send the 302 Found header along with the Location header
header('HTTP/1.1 302 Found');
header('Location: ' . $newLocation);
// Make sure that no other output has been sent before redirecting
exit;
?>
In this example: - The $newLocation variable stores the URL to which you want to redirect.
- The header() function is used to send the HTTP headers. The first header() call sends the HTTP status line indicating a 302 Found status, and the second header() call sends the Location header with the URL to which the client should be redirected.
- Finally, the exit statement is used to terminate the script execution immediately after sending the headers, ensuring that no further output is sent to the client.
Make sure this code is placed at the beginning of your PHP script, before any other output (e.g., HTML content) is sent to the client, as headers must be sent before any actual output. |