PHP Introduction | Page Transitions, Cookies, and Sessions | HTTP Headers and Page Navigation
Web applications use many technologies that differ from ordinary PC applications. We will explain how to use these Web-specific technologies in PHP. First, let us discuss “page navigation.”
On the Web, screens are composed of separate Web pages. The display changes by moving to other pages as needed. This is “page navigation.”
The important point is that when a page changes, the information from the previous page is cleanly removed. Of course, there are ways to keep information, which will be explained later. We previously submitted forms, and when a form is submitted, the target page is read again from the beginning and everything is created anew.
Therefore, when designing page navigation in a Web application, you need to keep in mind that previous information is cut off when moving between pages. You must design the application while considering how to preserve necessary information.
With that introduction done, let us explain page navigation. Page navigation is performed with a function called header. It is executed as follows.
header(content to output to the header);
Does this seem unclear? The header function outputs text to the “header” of the HTTP protocol. Why is that related to page navigation? Because when page navigation information is provided in the header, the browser that receives it performs a redirect.
You probably know that Web pages are transferred using a protocol called HTTP. HTTP is a set of rules that determines how a Web server and a client, meaning a Web browser, exchange information. Following this HTTP agreement and its rules, the server and browser exchange the information needed to access Web pages.
When a server sends a Web page, it first puts various information it wants to tell the browser into the header, then attaches and sends the body, which corresponds to the actual Web page. In other words, the header is “information that is not displayed as the page.” The header function outputs necessary information to this header area.
When moving pages, you send text like Location: destination address in the header. The browser that receives this Location header redirects to the delivered address. By providing this header, the browser that receives it accesses the specified page again and changes the page.
Moving Pages with the header Function
Let us create a simple page navigation example.
Contents of index.php
<?php
if ($_POST != null){
$url = $_POST['text1'];
header("Location:{$url}");
}
?>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>sample page</title>
</head>
<body>
<h1>Hello PHP!</h1>
<p><?php echo $url; ?></p>
<hr>
<form method="post" action="./index.php">
<input type="text" name="text1" value="./index2.php">
<input type="submit" value="Move">
</form>
<hr>
</body>
</html>
Contents of index2.php
<!DOCTYPE html>
<html lang="ko">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>sample page</title>
</head>
<body>
<h1>Index2.php!</h1>
<p>This is a newly prepared page.</p>
<hr>
</body>
</html>
Create these two files, index.php and index2.php, in the same location. When you press the button on index.php, you should see it move to index2.php.
When you actually try it, you may wonder, “How is this different from moving with an <a> tag?” But it is clearly different. An <a> tag simply moves to the specified page, while this approach first executes the PHP script written in the page and then specifies the destination inside that script. In other words, at the stage where the script is processed on the server, the server can tell the client that receives it to move to this page. Therefore, required processing and page navigation can be combined.
One point to be careful about with page navigation by header is that after header is executed, the later processing is effectively canceled. This is because when the browser receives the header, it moves to that page, so anything written after that becomes meaningless. Perform all processing before writing Location with header.
Another important point is that header must be executed before anything is output. This is a common issue. Since headers are sent before the body, if echo, HTML tags, or anything else tries to output something, the headers are automatically sent first at that point. Once headers have been sent, another header cannot be resent.
Therefore, the header function must always be executed before outputting anything. Since PHP scripts are written inside HTML tags, anything written outside <?php ?> tags is output immediately as is. For example, if even a newline exists before the <?php ?> tag, the header function can no longer be used because the newline code has already been sent and the headers are already transmitted. Make sure you understand this point.