PHP Include and Require Statements, and Header() Function
Differences between include and require statements
vs. header() function:
include and require statements:
- forward request from one PHP file to another
- all processing takes place on the server
- one round trip: client > server > client
header() function:
- redirect request (e.g., PHP file must run itself again, as in form processing)
- some processing takes place outside the server
- sends HTTP HEADER back to browser (e.g., Location header
redirecting browser to another URL)
- two round trips: client > server > client > server > client
PHP include and require statements (constructs):
In PHP, content can be inserted from one PHP file into another PHP file, before the server executes the original file. The include and require constructs are used to insert necessary code written in other files, pertinent to the flow of execution.
Include and require are identical, except upon failure:
require will produce a fatal error (
E_COMPILE_ERROR) and stop the script
include will only produce a warning (
E_WARNING) and the script will continue
If execution must continue to display content to users, even if the include file is missing or there is an error in the include file, use
include. Otherwise, in case of FrameWork, CMS or a complex PHP application coding, always use require to include a key file to the flow of execution. This will help avoid compromising your application's security and integrity, just in-case one key file is accidentally missing.
There are many uses for include files: including header, footer, or menu files for all web pages, or even a database connection file. When one of the files needs to be updated, it is only necessary to update include file.
Saves a lot of work!
Syntax:
include 'path_to_filename';
or...
require 'path_to_filename';
Example:
<html>
<body>
<?php include 'menu.php'; ?>
<h1>Welcome!
<p>...</p>
</body>
</html>
Example: Include
PHP header() function:
On the other hand, the header() function is used to send raw HTTP header information. It's an important distinction to know that the header() function must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP. In fact, it is a very common error to read code with include, or require, functions, or another file access function, and have spaces or empty lines that are output before the header() function is called. The same problem exists when using the header() function in a single PHP/HTML file.
When requesting that a web page is brought back to your browser, more
than just a web page is returned. An HTTP HEADER, which includes
additional information, is returned. HTTP HEADER information includes the type
of application making the request, date requested, whether it should be
displayed as an HTML document (or another MIME type), the length of the
document, as well as more information.
One of the things HTTP HEADER does as well is give status
information: that is, whether the page was found (404 errors), and the
location of the document. Here's an example to redirect users to another page:
<?PHP
header("Location: http://www.qualityinstruction.com/");
//exit *must* be included after the header() function
exit;
?>
<html>
<body>
</body>
</html>
Note:
The header() function call *MUST* go before any HTML, or blank lines in a file.
If header() function is
called after the HTML, an error will be generated, roughly: "Cannot modify header information."
Note:
HTTP/1.1 requires an absolute URI as argument to Location: including
the scheme, hostname and absolute path, but some clients accept
relative URIs. You can usually use $_SERVER['HTTP_HOST'],
$_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a
relative one yourself:
<?php
/* Redirect to a different page in the current directory that was requested */
$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']), '/\\');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
//exit *must* be included after the header() function
exit;
?>
Side Note:
A uniform resource identifier (URI) is a string of characters used to
identify a name or a resource. Whereas, a URL specifies a
network
location. Therefore, a URL is a URI that, in addition
to identifying a network resource, includes the path to the resource
through a network location.
Note:
Session ID is not passed with Location header even if session.use_trans_sid is enabled. It must by passed manually using SID constant.
References:
PHP Include - PHP Manual
PHP Require - PHP Manual
PHP header() Function - PHP Manual
PHP Include Files - W3 Schools
Uniform Resource Identifier