Tuesday, October 20, 2009

PHP Include Tutorial

PHP Include Tutorial – Basics Of The Include Language Construct
It should be noted that, much like the Echo statement in PHP, Include is not considered an actual function. Although it would behave like a function, we call it a language construct since it is “built into” PHP. We consider it an integral part of the language much like the IF statement! (You may notice some language constructs listed as functions- but this is only to improve documentation.)
The primary usage of this language construct in particular is to retrieve a remote file for inclusion into the currently running script. We are going to use the Include construct for three primary reasons:
1. Readability – We use the Include construct for replacing many lines of code with but one line of code. This increases the readability of an application, and ultimately lets us troubleshoot bugs quicker than ever before. We use the Include construct for replacing many lines of code with but one line of code. This increases the readability of an application, and ultimately lets us troubleshoot bugs quicker than ever before.
2. Save Time – We save time by using the Include() construct in a very ingenious way. If we were to change a link in our navigation menu, we would have to update that same link hundreds of time on different subpages. But if we used an Include() construct, we could just edit one file and the results on other pages would be instant!
3. Reliability – If an included file doesn’t exist or has malfunctioned, the server will tell us quite promptly. This is superb for troubleshooting! Error reporting is sometimes seen as a security risk, however, since it commonly shows semi-confidential information in the error.
PHP Include Tutorial – Examples Of Including A File
Before we can do dabble with an example, we need to learn how the Include syntax works. Using the Include construct is actually quite easy; we just need to know the URL of the code to be input, wrap it in parenthesis, and add the include statement.
An Example Of The PHP Include Syntax
include("header.php");
include 'footer.php';

// Each Way Is Correct, But The First Example Is More Readable
?>
In the above example we are calling to two different files, in which both are located in the same directory as the page being viewed. Both statements will work, but we personally like the first example since we view it as more readable.
If the file we wanted to include is in the parent directory, we simply use the “../” notation as seen below.
Including A PHP File From A Parent Directory
include("../header.php");

// Include A File From Parent Directory
?>
Note that every time we use the “../” notation we go up one level. But things don’t have to get complicated! We can simplify things by simply using the entire URL as seen below:
An Easier Way To Include A PHP File
include("http://www.YourUrl.com/includes/header.php");

// Easy File Inclusion
?>
PHP Include Tutorial – An Include Example
To get started we are going to need at least two files. First, we will take an already-made index.php file as seen below:
Contents Of index.php – Copy And Paste

A Basic PHP Website Using Include


Home
Subpage 1
Subpage 2



Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.







Now create a file called “header.php” in another window. We are going to take out some of the contents of the index.php file and replace it with our include statement. Our header.php file should look like the following:
Contents Of header.php – Copy And Paste
Home
Subpage 1
Subpage 2
?>
Now we have two separate files. But we have a problem- how do we get the URLs from the header.php file into the index.php file? Simple! Review the code below to see where we put the include statement:
Contents Of header.php – Copy And Paste

A Basic PHP Website Using Include



include("header.php"); ?>




Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.





Now upload both files to a same directory and test it out! If you’d like, you can change around the contents of the header.php file, upload it to your web server, and see the changes in real-time. This may not seem like a big time saver with only one page to use it on, but if we were to have a website filled with 100 pages, this would be quite the little time saver!
PHP Include Tutorial: Dealing With Variable Scope
Variable scope sounds like a mean phrase to anyone who doesn’t know what it is. The scope of a variable is just how far it “reaches”- or basically where it can and can’t be accessed. Variable scope can be seen when looking at two separate PHP scripts. If they are not connected in any way, then the variables of the first script will not interfere with variables in the second. (And vice versa)
We deal with scope in the Include construct quite simply because we are merging two files- so if we are using the same variable name in both scripts, which one gets precedence?
In our previous example we are not dealing with scope since we aren’t using any variables. But we can change that with the following edits to both index.php and header.php:
header.php – Edits To Show Variable Scope
$home = "Home";
$sub1 = "
Subpage 1";
$sub2 = "
Subpage 2";
?>
We just declared our three variables as our links, so that we may see which variables take precedence in our index.php file. Our index.php file should be updated as seen below:
Contents Of index.php

A Basic PHP Website Using Include




$home = "";
$sub1 = "";
$sub2 = "";
include("header.php");

echo "$home $sub1 $sub2";
?>



Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.





So what do you think the script will output to the browser? As you can see, we declared each variable as an empty string before we included the file. But in the include of the file, we stated that the variables should contain links.
Upon testing this script, one can see that the output is going to be successful in displaying the correct navigation links. From this example it is seen that an included file will indeed overwrite variable values unless otherwise coded.
It should also be made clear that an include file is only going to have access to variables and functions that have been declared before the line used to include the file. In the following example we will declare a variable and reference it within the include file. (No need to test this out unless you want- you can easily see the results with the two short snippets below)
index.php – Testing Variable Scope
$hello = "hello world!";
include("echo.php");
?>
Now for the echo.php file:
echo.php – Testing Variable Scope
echo $hello;
?>
Once this script is run, we will see that “hello world!” is output to the screen. If we were to put the variable after the include line, we wouldn’t get any results at all.
PHP Include Tutorial – Two Major Security Concerns
PHP is considered a generally secure language since it hides the contents of the PHP code from the user. (Go ahead and view the source of the HTML of pages we have created- you won’t see PHP code!) If we take this protection away, anyone can see the contents of our scripts. Luckily you have to make the mistake of not using the PHP extension for this to happen.
It is sometimes common to see some use plain text or even a .INC file type for their includes. If you use the wrong extension or remove the PHP tags from the header.php file we were working with, you’ll notice that the plain text is output to the screen if you try to access header.php directly. This goes to show that when we use includes, always remember to use the PHP tags to enclose the data if it is using any type of sensitive data. (Or any time at all, since it builds good programming practice!)
The second major security concern is including files from a remote location. If we are only using local resources on our website, such as template files we have created, there is no reason to fret. But if we are pulling information from other websites, which is sometimes illegal in many cases anyway, the remote website can inject code into our websites and get confidential information with ease. As such, developers should only agree to use includes from remote resources if they are to be trusted without doubt.

Ref: learnphponline.com

No comments:

Post a Comment