|
|
|
PHP Includes
include( ) and require( )
- So far we have created scripts consisting of a single file, with all the code we need included in that file.
- When you develop a large site, you will find that to be very inefficient.
- For instance, if we have a navigation bar at the bottom of each of 100 web pages, and we need to make an addition to that nav bar, we would have to go through every page to change it.
- Fortunately, PHP allows us to make use of external files, which allows you to divide your script into distinct parts. We can then assemble those parts together in a way which gives us the page we want.
- If we need to change something that applies to all the pages of a site, then all we need to do is change that one file.
PHP has four functions for using external files:
- include( filename.inc )
- include_once( filename.inc )
- require( filename.inc )
- require_once( filename.inc )
- When you use one of these functions, it takes the contents of the included file and places it at the location of the function call.
- The included file inherits the variable scope of the line on which it occurs.
- So, if an include is placed inside a user-defined function, its scope would be local to the function from which it is called.
- If the include is placed in the general body of a document, on the other hand, it has global scope.
- That means any variables you have defined in the include are available to the rest of the script, and vice versa.
- The included file may contain DTD, HTML, JavaScript, even PHP. It may also include other includes.
- If it includes PHP, however, that code must be placed between the <?php ?> tagset.
Save the following as header.inc:
1 |
<html> |
2 |
<head> |
3 |
<title><?php echo $page_title; ?></title> |
4 |
</head> |
5 |
<body> |
6 |
<?php $name=’Steve’; ?> |
Now create the PHP document:
1 |
<?php |
2 |
$page_title = “Include Test”; |
3 |
include( ‘header.inc’); |
4 |
?> |
5 |
<p>Hello <?php echo $name;?></p> |
6 |
</body> |
7 |
</html> |
- Because we define $page_title before we call the include, the variable is available to the included code.
- Likewise, because $name is defined in the include, it is available to subsequent code.
- The included file can have any extension.
- By practice, many programmers use .inc .
- However, do not put anything sensitive in a .inc file – it may easily be viewed by a user.
- If you are including sensitive information in an include, give it a .php extension. If someone tries to open it directly, it will be parsed.
- Includes may be placed inside conditionals, so they fire only if certain conditions are met.
This function also has an include_once( ) version. This insures that the file is included only once, in case you have a script that calls the include repeatedly.
The functions require( ) and require_once( ) do exactly the same things as include with one difference – the way it handles errors.
- If an include( ) cannot load a file for some reason, it will give you a warning, but the rest of the script will execute.
- If a require( ) cannot load the indicated file, you get a fatal error, and the script stops executing.
You might want to use require( ) for files that are essential for your script to run, such as those that connect to a database. Use include( ) for files that aren’t that essential.
You can suppress the warning that occurs when an include( )fails by pre-pending the function call with a @
@include( filename.inc );
If you use a number of programmer-defined functions in all your scripts, put them in one file and use the require( ) function to access them.
HTTP Headers
- HTTP – HyperText Transfer Protocol – is the set of rules used by the web to define the way clients and servers communicate.
- When a browser receives a response from a server, part of that response is a series of HTTP headers, which tell the browser what kinds of files are to follow.
- PHP has a built-in header( ) function which uses this protocol.
- One of the most common uses for header( ) is to redirect the browser from the current page to another page.
- The critical thing to remember when using the header( ) function is that is must be called before anything else is sent to the browser.
- That means you cannot use echo( ) or print( ), send any HTML, have any blank lines outside the PHP tags, have even one blank space before the header is sent, or have any includes which do any of the above.
- Otherwise you will receive a warning saying that headers have already been sent.
To use header( ) to redirect the browser
header( “Location: http://www.yahoo.com” );
HTTP requires an absolute URI as an argument to Location.
You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'], and dirname( ) to create an absolute URI out of a relative URI.
<?php
header("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF'])
."/".$relative_url);
?>
http:// + serverName + DirectoryName + filename
- Remember, header( ) must be called before any actual output is sent, including HTML, blank lines, blank spaces.
<html>
<?php
/* This will give an error. Note the output
above, which is before the header( ) call */
header('Location: http://www.example.com/');
?>
- PHP has another function available called headers_sent( ) which checks to see whether data has already been sent to the browser.
if( !headers_sent( ) ){
header( “Location: http://www.yahoo.com” );
}

|
|