How To Use The Header() Function In PHP

How To Use The Header() Function In PHP

·

1 min read

In this tutorial, we will be covering how to use header(), a PHP built-in function. When we want to redirect the user to a different page on our website, we use the built-in header() function.

Syntax

header("Location: pagename.php");

So whenever we want to redirect the user from the current page to another page on our website, you simply write header("Location:pagename.php"); and the user will be redirected to the page with the name of pagename.php.

One Important Note

One small glitch that you need to pay attention to is the code that comes after (if any) header("Location:pagename.php") will keep executing even after the user has been redirected!

By default, header("Location:pagename.php") does not end the execution of the remaining PHP code on the page. There are times when this will produce some effect that you do not want.

To prevent any code below from executing, remember to add exit() after the header() function.