Right now you have a page that have html tag, therefore it seems like you have Response status 200 OK, you only set header with Location
in it. I would probably go in a slightly different way:
<?php
if (isset($_POST['link-btn'])) {
header("Location:page.php", true, 301);
} else {
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<section id="main">
<form method="post">
<button type="submit" name="link-btn">Next page</button>
</form>
</section>
</body>
</html>
<?php } ?>
What you really need to do is to create this type of HTTP Response
https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301
I believe that you have writen the PHP code into the HTML <head>
element, because you know that this contains header. But be aware in here. You have HTTP Response (or HTTP Request) with headers. And then you have headers in your HTML, which is the body (not header) of the HTTP Response. Therefore you are setting header()
in PHP for the HTTP Response, but that is fine anywhere in the code before the code is sent (that's when you get common error "headers have already been sent"). Once the HTTP Response is out there, you can not modify it. Therefore it's the best to create all of it at once, then create HTTP Response and then send it.
If you look here
https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/HttpFoundation/Response.php#L241
you will see method __toString
that actually creates the HTTP Response itself, this is what browser is getting from the server. To learn this it's the best way how to understand the web :)