Before you start
In this tutorial you will learn how to use sessions with PHP, how to manipulate XML data with the DOM, and how to create, use, and read JSON data in PHP.
This tutorial teaches you how to use PHP by demonstrating the construction of a web-based workflow application. "Learning PHP, Part 1" covered the basics, such as syntax, functions, working with HTML forms submissions and databases, and creating a process by which a new user can register for an account.
In this tutorial, you will enable users to upload files to the system by using their browsers, and you will use first XML, then JSON to store and display information about each file.
Part 3 looks at using HTTP authentication, as well as protecting files by streaming them from a non-web-accessible directory. You'll also look at creating objects and using exceptions.
In the course of this tutorial, you'll examine:
- Creating and using sessions and session information.
- Uploading files from the browser.
- Creating XML using the Document Object Model (DOM).
- Manipulating XML data using DOM.
- Reading and using JSON data.
This tutorial is Part 2 in a three-part series designed to teach you various aspects of working with PHP as you build a workflow application. Take this tutorial if you have a basic understanding of PHP and want to learn about uploading files from the browser, sessions, or using PHP to process XML or JSON.
This tutorial assumes a basic familiarity with PHP to the level discussed in Part 1 of this series. That includes basic understanding of control structures, such as loops and if-then statements, as well as functions and working with HTML form submissions and databases. Familiarity with XML is helpful, but not required. (You can find more information about these topics in Resources.)
You need to have a web server, PHP, and a database installed and available. If you have a hosting account, you can use it as long as the server has PHP V5 installed and has access to a MySQL database. Otherwise, download and install the following packages:
- Whether you're on Windows®, Linux®, or even Mac, the easiest way to get all of the necessary pieces of software for this tutorial is to install XAMPP, which includes a web server, PHP, and the MySQL database engine. If you choose to go this route, install and then run the control panel to start up the Apache and MySQL processes. You also have the option of installing the various pieces separately. Keep in mind that you then have to configure them to work together—a step already completed with XAMPP.
- Web server
- If you choose not to use XAMPP, you have several options for a web server. If you use PHP 5.4 (as of this writing, XAMPP is only using PHP 5.3.8) you can use the built-in web server for testing. For production, however, I assume that you're using the Apache Web server, version 2.x.
- PHP 5.x
- If you do not use XAMPP, you need to download PHP 5.x separately. The standard distribution includes everything you need for this tutorial. Feel free to download the binaries; you do not need the source for this tutorial (or ever, unless you want to hack on PHP itself). This tutorial was written and tested on PHP 5.3.8.
- Part of this project involves saving data to a database, so you need a database as well. Again, if you install XAMPP, you can skip this step, but if you choose to, you can install a database separately. In this tutorial, I concentrate on MySQL because it's commonly used with PHP. If you choose to go this route, you can download and install the Community Server.