How can we redirect from one webpage to another webpage in PHP? (PHP redirect to page)

In some situation, we may want to redirect user from one webpage to another webpage using PHP. So how can we redirect?

PHP redirect to page

To redirect user from one webpage to another webpage we use "header" function. 

Suppose I want to redirect user from webpage1.php page to webpage2.php

Then, at the top of webpage1.php (before html tag), I am going to call the header function. 

<?php
header('Location: webpage2.php');
?>
<!DOCTYPE html>
<html>
<head>
<title>PHP Redirect</title>
</head>
<body>
<h1>Webpage 1</h1>
</body>
</html>
Do the presence of header function matters?

Yes. Absolutely. We have to call the header function before anything prints to the webpage. Thats why we have called header function at the top of the webpage. It will redirect the user from one webpage to another webpage.
How can we redirect from one webpage to another webpage in PHP? (PHP redirect to page) How can we redirect from one webpage to another webpage in PHP? (PHP redirect to page) Reviewed by Pixel Stream on December 09, 2019 Rating: 5

No comments:

Powered by Blogger.