- PHP

How to use header() in PHP for Redirect

You can use the header() function to make some backend redirection. In most cases, it’s the best way to redirect your users, because it works server-side.

Remember, you shouldn’t be printing any content before header() redirection, because your redirection may not work.

<?php header("Location: https://snippetsdb.com/"); ?>

You can also redirect to your specified files.

<?php header("Location: path/to/your/file.php"); ?>

Example:

Redirect the user with header() function when the form is submitted.

<form action="" method="post">
    <input type="text" name="test">
    <input type="submit">
</form>

<?php

if(!empty($_POST)) {
    header("Location: index.php");
}

?>