PHP Web Application Development

When you've gotten the basics down, and you've learned to communicate with a database, it's not too early to jump right into some simple application development, and then you can learn and refine from there. What takes you from programming a single page to developing a full-scale application is essentially learning to pass information from page to page and to manage state during a user's browsing session. When users click on links to see database-driven content (like a link in a list of weblog entries, or a user's profile on a social networking site), you'll likely need to pass GET parameters through the URL. If they're filling out a form to be processed on the next page, you'll likely use POST to pass the form data. If you're logging them in, you'll probably use the SESSION to save information about who the current user is, so that subsequent pages are aware of the user's logged-in state until they leave the site. Each of these "scopes" of global variables are already built in to the PHP/Apache environment, so it's less about learning HOW to use them is more of a matter of learning WHEN to use them. Below, you'll see ex

Globals for passing information, handling forms, and maintaining state
_GET: Grabbing Variables from the URL

The $_GET variable is a global that is available to any page, and contains the variables that have been specified in the URL when the page loaded. This is most commonly used when linking a user to certain content on a site (like view_weblog_entry.php?weblog_id=3 or see_user_profile.php?user_id=14). It is NOT typically used for submitting a form to be processed (see the section about $_POST below), and most certainly not for processing logins, since the information passed is visible in the URL. Below you'll see a simple example of a page code that outputs the value for "x" that is passed in the "query string" of the URL (the stuff after the "?"). Note that the parameters in the URL are separated by the "&" symbol.

Try it: code/GlobalGet.php?x=1&y=2

_POST: Grabbing Variables from Form Posts

The $_POST variable is another global that contains all information sent by a form (if the 'method' in the form tag was 'post') on the previous page. The variable names are whatever was assigned as the 'name' of each input tag in the form, and their values are whatever was contained in the 'value' (or the selected option(s)) when the form was filled out and submitted. These inputs could include hidden, text, textarea, select, checkbox, radios, etc. Below you'll see a form that includes several of these elements, and the code for an output page that grabs their information for processing and display.



Checkbox 1 Checkbox 2 Checkbox 3


Checkbox 1 Checkbox 2 Checkbox 3
_SESSION: Remembering Variable Values Throughout a Browsing Session

The $_SESSION array is a variable that holds information made available on each page by matching cookies stored on the user's system and used to match them to a current browsing session on each page they visit. By saving from - and reading to - the $_SESSION variable, information about the user and their browsing experience can be saved for use on other pages without having to pass the information through forms and URLs. To demonstrate this particular effect, fill out the form below (a simple text input), and submit the page. You'll see some output on the result page indicating that something has happened. Close that page and then refresh this one, and you'll see that the information saved from your form input on the other page is available to this one, because you're still browsing in the same session.

First enter any string and submit:
Database-Driven Applications Using MySQL

COMING SOON: In this section, we'll set up some wrappers to include in all files (thus making certain site-wide markup and functionality readily available on all pages without too much clutter). We'll then develop a simple user login system, including building a table for user data, creating a login form, and writing some code to handle a login and then maintain the logged-in state throughout the rest of the site.

Getting Started


Recommended next: Object-Oriented PHP