PHP

Redirect users to another page

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:

Redirect to another page

PHP
<?php
header("Location: /folder/page.php");
exit();
?>

Redirect if the page has been moved permanently

PHP
<?php
header("Location: http://www.domain.com/another-page.php", true, 301);
exit();
?>

Notes:

  • 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.