How to redirect to another page in PHP?

In this article, we will see how to move from one page to another automatically in PHP. Redirection is needed where the action is successfully done and we need to move to the starting pages or where our next objective pages.

What is a header()?

In PHP, the header() method is used to move the location from one resource to another. It is used to change the location to another page which maybe another local page or any webpage.

How the header() redirect to a local page?

The syntax to redirect the page control to a local page is given below,




header("Location:page_name");

Let us illustrate an example,

<?php
  //redirects to a local page called redir.php
 header("Location: redir.php"); 
?>

redir.php

<?php
echo"This is the redirected page";
?>

Output:-

This is the redirected page

How the header() redirect to a webpage in PHP

The syntax to redirect the page control to a webpage is given below,

header("Location:page_url");

Let us illustrate an example,

<?php 
//redirects to codespeedy's homepage
header("Location: https://www.codespeedy.com"); 
?>

Output:-

redirect in PHP

In this way, we can redirect to another page using PHP. If you have any doubt, put a comment below.

See also,


Leave a Reply

Your email address will not be published. Required fields are marked *