Putting PHP and MySQL to work
Steve shows how to create personalized Web sites that let visitors customize their view of the site and the information they see. This server-side application, for PHP developers with some experience, stores user profiles in a MySQL database and dynamically generates information using PHP. Steve also introduces the new session management features in PHP4 and discusses efficient PHP coding.
The days of static Web sites are gone. Most major Web sites now offer the ability for each visitor to customize the site to their liking. Sites can now save information personalized to the needs of each visitor such as how they view the pages, their profile information, billing and shipping instructions, a list of favorite pages or products, and so on. When the visitor returns the next time, the site generates Web pages dynamically that take these preferences into account.
How do these sites work such magic? The answer lies in server-side scripting languages, such as PHP, ASP, and JSP. In this article, I show how you can create a Web site that features a sidebar with news links to stories from other sources. This layout works nicely with any type of Web site, because it leaves the rest of the page free for any type of content. Site visitors who create profiles can select the news sources they want to display and the colors they want to use for site structural elements. Visitors who do not create their own profiles see a generic layout.
In this example, a link at the top of the Web page brings visitors to the login page, where they can sign in or create a new account. To create a new account, the visitor enters an e-mail address and a password. The system sends a confirmation to the e-mail address that includes a link containing the confirmation code. When the visitor uses the link, he or she can confirm the e-mail address and password entered previously. If the information entered is valid, the visitor is sent to the user profile page where to set preferences. Once the visitor has edited a profile, he or she can return to the homepage to view it using those preferences.
This article assumes that you are already familiar with basic PHP syntax and function. If you need an introduction, read Craig Knudsen's introductory article on PHP, which I've included in the Resources section. I use the new session management features that are only available with PHP version 4. I also assume that you are familiar with MySQL user and database management. If not, I recommend the MySQL Administration tutorial offered by DevShed.com (also in the Resources section). Keep in mind that MySQL has recently been relicensed under the GNU Public License, which means that it is now free on all platforms, including Microsoft Windows.
To store visitor information and preferences, you need to create a database and design the tables that will hold the information. All information will be entered into the database solely through a Web interface. When visitors fill out forms to sign up for an account or change their account, the PHP scripts will perform the appropriate SQL queries on the database.
All the pages on the Web site will be generated by PHP code and will vary if the visitor is logged in. Even though the initial page might look quite different for each user, you will see that a single program calls the external functions to create the personalized appearance.