To redirect a user to another page using PHP, you can use the header() function. This function sends a raw HTTP header to the browser, which can be used to redirect the user to a new page. Here’s are two simple examples:
<?php
header("Location: /folder/page.php");
// Make sure no further code is executed after the redirect:
exit();
?>
<?php
header("Location: http://www.domain.com/another-page.php", true, 301);
// Make sure no further code is executed after the redirect:
exit();
?>
The header()
function must be called before any output is sent to the browser, including whitespace or HTML tags. If you have any output (such as echo or print statements) before calling header()
, it will result in an error.
If you're getting issues with headers already being sent, ensure you call the header()
function at the beginning of the script, before any HTML content or whitespace.