Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your profile (name, country/region, and company) is displayed to the public and will accompany any content you post. You may update your IBM account at any time.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Learning PHP, Part 1: Register for an account, upload files for approval, and view and download approved files

Nicholas Chase, Founder, NoTooMi
Nicholas Chase is the founder and creator of NoTooMi. In addition to technical writing for large corporations, he has been involved in website development for companies such as Lucent Technologies, Sun Microsystems, Oracle, and the Tampa Bay Buccaneers. He has been a high school physics teacher, a low-level-radioactive waste facility manager, an online science fiction magazine editor, a multimedia engineer, an Oracle instructor, and the chief technology officer of an interactive communications company. He is the author of several books, including XML Primer Plus (Sams 2002).

Summary:  This tutorial is Part 1 of a three-part "Learning PHP" series teaching you how to use PHP through building a simple workflow application. This tutorial walks you through creating a basic PHP page using HTML forms and covers accessing databases.

23 Oct 2012 - Nicholas Chase updated content throughout this tutorial to reflect current PHP technology.

View more content in this series

Date:  23 Oct 2012 (Published 14 Jun 2005)
Level:  Intermediate PDF:  A4 and Letter (948 KB | 38 pages)Get Adobe® Reader®

Activity:  85066 views
Comments:  

Cleaning up: including files

So far, each script you've written has been self-contained, with all of the code in a single PHP file. In this section, you'll look at organizing your code into multiple files. You'll take sections of code that you use on multiple pages and place them into a separate file, which you'll then include in the original pages.

Why include files?

PHP provides two ways to include files. One is for including support files, such as interface elements, and the other is for crucial files, such as functions called within the page.


Including the definitions

Start by creating the files you'll eventually include. Whenever you create a website, one of the first things you need to do is create a header and footer file that contains the major interface elements. That way, you can build as many pages as you want without worrying about what the page looks like until the coding work is done. At that point, you can create the interface just once, in the include files, and the entire site will be instantly updated.

To start, create a file called top.txt and add the code in Listing 43.


Listing 43. Creating a file called top.txt

  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <title>Workflow Manager</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>
  <body>
    <div id="wrapper"><div id="bg"><div id="header"></div>
      <div id="page"><div id="container"><div id="banner"></div>
        <div id="nav1">
          <ul style='float: left'>
            <li><a href="#" shape="rect">Home</a></li>
            <li><a href="#" shape="rect">Upload</a></li>
            <li><a href="#" shape="rect">Files</a></li>
          </ul>
        </div>
        <div id="content">
          <div id="center">

In a separate file called bottom.txt, add the following shown in Listing 44.


Listing 44. bottom.txt

          </div>
        </div>
      </div>
    </div></div></div>
  </body>
</html>

Save both files in the same directory as registration.php. Copy the images directory and the style.css file from the PHPPart1SampleFiles.zip in Downloads to that directory as well, as they're referenced by the HTML code in top.txt.


Including the files

Now go ahead and add these files to the registration page. Edit registration.php to look like Listing 45.


Listing 45. Adding top.txt and bottom.txt to the registration page

<?php

   include("top.txt");

?>

<h1>Register for an Account:</h1>
<form action="registration_action.php" method="POST">
   
   Username: <input type="text" name="name" /><br />
   Email: <input type="text" name="email" /><br />
   Password: <input type="password" name="pword[]" /><br />
   Password (again): <input type="password" name="pword[]" /><br />
   <input type="submit" value="GO" />

</form>

<?php

   include("bottom.txt");

?>

Notice that you've removed the HTML that normally surrounds the content of the page and replaced it with a command to include the files you just created. Now it's time to see what that action does.


The results

If you now point your browser back to the registration page, you will see a much different look, as shown in Figure 15.


Figure 15. New look of registration page
New registration page

If you do a "view source" on the page, you can see that all three files have now been merged in the output (see Listing 46).


Listing 46. Files have been merged in the output

  <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <title>Workflow Manager</title>
    <link rel="stylesheet" type="text/css" href="style.css" />
  </head>
  <body>
    <div id="wrapper"><div id="bg"><div id="header"></div>
      <div id="page"><div id="container"><div id="banner"></div>
        <div id="nav1">
          <ul style='float: left'>
            <li><a href="#" shape="rect">Home</a></li>
            <li><a href="#" shape="rect">Upload</a></li>
            <li><a href="#" shape="rect">Files</a></li>
          </ul>
        </div>
        <div id="content">
          <div id="center">
<h1>Register for an Account:</h1>
<form action="registration_action.php" method="POST">

   Username: <input type="text" name="name" /><br />
   Email: <input type="text" name="email" /><br />
   Password: <input type="password" name="pword[]" /><br />
   Password (again): <input type="password" name="pword[]" /><br />
   <input type="submit" value="GO" />

</form>

          </div>
        </div>
      </div>
    </div></div></div>
  </body>
</html>

If you go ahead and make the same changes to registration_action.php and submit the form, you'll see that the changes take place immediately.

Now, this page isn't a work of art, and that's OK. Later, you can get a designer to make it look nice, and you'll have to make the changes only once—to the included files—rather than to every page on the site.


Requiring files

If PHP can't find interface files, it's a problem, but it isn't necessarily a catastrophe, especially if all you're worried about is the functionality of the application. As a result, if PHP can't find a file specified by the include() function, it displays a warning and continues processing the page.

In some cases, however, not being able to find an include file is a catastrophe. For example, you can pull the validate() script out into a separate file and include that file in the registration_action.php file. If PHP can't find it, that's a problem because you're calling that functions within the page. So, to avoid that, you can use the require() function, instead of include() (see Listing 47).


Listing 47. Using the require() function



<?php

   include("top.txt");
    require("scripts.txt");

?>

<p>You entered:</p>

<?php
   foreach ($_POST as $key=>$value) {
      echo "<p>".$key." = " . $value . "</p>";
   }

   $passwords = $_POST["pword"];
   echo "First password = ".$passwords[0];
   echo "<br />";
   echo "Second password = ".$passwords[1];

   if (validate($_POST) == "OK") {
      echo "<p>Thank you for registering!</p>";
...

If PHP can't find a page that's required, it sends a fatal error and stops processing.


Avoiding duplicates

There's nothing to stop you from including a file in a file that is itself included in another file. In fact, with all of these include files floating around, it can get confusing, and you may inadvertently include the same file more than once. This duplication can result in interface elements appearing multiple times, or errors due to the redefinition of functions or constants. To avoid that, PHP provides special versions of the include() and require() functions. For example, you can be sure that the registration_action.php file will load the files only once (see Listing 48).


Listing 48. Ensuring that the registration_action.php file will load the files only once

<?php
   include_once("top.txt");
   require_once("scripts.txt");
?>

<p>You entered:</p>

<?php
...

When PHP encounters the include_once() or require_once() function, it checks to see if the file has already been included in the page before including it again.

6 of 10 | Previous | Next

Comments



static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=Open source, Linux
ArticleID=133648
TutorialTitle=Learning PHP, Part 1: Register for an account, upload files for approval, and view and download approved files
publish-date=10232012
author1-email=ibmquestions@nicholaschase.com
author1-email-cc=