PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY If you plan to use InnoDB, you need to tell MySQL where to store the data. The InnoDB engine uses a single tablespace that acts as a sort of virtual file system. InnoDB files, once created, cannot be made smaller. The default location for the tablespace is C:Program FilesMySQLMySQL Server 5.0data. If you want to locate the tablespace elsewhere, the drop-down menu offers some suggested alternatives. When you have made your choice, click Next. 6. Leave the next dialog box at the default Decision Support (DSS)/OLAP and click Next. 7. The next dialog box sets the networking options and SQL mode. The important settings are in the top half. Make sure Enable TCP/IP Networking is checked, and leave Port Number on the default setting of 3306. The lower half of the dialog box lets you choose whether to run MySQL in strict mode. In an ideal world, you should accept this default setting, but it may cause problems with some PHP applications written before strict mode was introduced. Deselect the Strict mode check box and click Next. 8. MySQL has impressive support for most of the world s languages. The next dialog box invites you to choose a default character set. In spite of what you might think, this has no bearing on the range of languages supported all are supported by default. The character set mainly determines the order in which data is sorted. Unless you have a specific reason for choosing anything other than the default Standard Character Set, I suggest that you accept it without making any changes, as shown. Click Next. 9. The recommended way of running MySQL is as a Windows service. If you accept the defaults as shown in the top half of the next dialog box, MySQL will always start automatically when you boot your computer and run silently in the background. (If MySQL has already been installed as a Windows service, this section will be grayed out.) If for any reason you don t want MySQL to start automatically, uncheck the Launch the MySQL Server automatically option. You can easily change this option later (see the section Starting and stopping MySQL manually on Windows later in this chapter). We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.
SETTING UP MYSQL AND PHPMYADMIN 10. The actual installation now takes place and is normally very quick. When everything s finished, you re presented with a final dialog box. If this is a new installation or if you are upgrading from one series to another, click Finish to launch the Configuration Wizard, which is described in the next section. If you are upgrading to a later version of the same series (such as from 5.0.10 to 5.0.24), deselect the check box labeled Configure the MySQL Server now before clicking Finish. MySQL should be ready to use, but needs to be restarted manually (see Starting and stopping MySQL manually on Windows later in the chapter). If you have a software firewall, you might also be prompted to allow connections to and from MySQL. Configuring MySQL Windows Essentials There are a lot of dialog boxes to go through, although all you usually need to do is accept the default setting. These instructions are based on version 1.0.8 of the Configuration Wizard. 1. The Configuration Wizard opens with a welcome screen. Click Next to proceed. 2. The first dialog box asks whether you want a detailed or standard configuration. Choose the default Detailed Configuration option and click Next. 3. The three options on the next screen affect the amount of computer resources devoted to MySQL. Accept the default Developer Machine and click Next. If you choose either of the other options, all other programs will slow down to a crawl. 4. The next dialog box asks you to select from the following three types of database: Multifunctional Database: Allows you to use both InnoDB and MyISAM tables. Transactional Database Only: InnoDB tables only. MyISAM is disabled. Non-Transactional Database Only: MyISAM tables only. InnoDB is disabled. Most hosting companies support only MyISAM tables, so choose Non-Transactional Database Only. Unless you plan to learn MySQL in depth, there is little advantage in choosing Multifunctional Database, which requires an extra 30MB of disk space. If you choose Multifunctional Database, you need to edit the MySQL configuration file later, as described in Changing the default table type on Windows Essentials. 5. What you see next may vary. If you chose Non-Transactional Database Only in the preceding step, you should be taken directly to step 6. However, you may see a dialog box inviting you to select a drive for the InnoDB data file. Unless you chose Multifunctional Database, just click Next and move on to step 6. 265 Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Installing the Windows Essentials version of MySQL These instructions are based on the 5.0 series of MySQL, which is installed in C:Program FilesMySQLMySQL Server 5.0. I expect MySQL 5.1 to become the rec- ommended release shortly after publication of this book. On past experience, the default location changes for each series of Windows Essentials, so 5.1 is likely to be installed in C:Program FilesMySQLMySQL Server 5.1, and Windows treats different series as completely separate programs. If you upgrade from one series to another, any existing databases need to be transferred to the new version as if it were a different server (see the section titled Backup and data transfer near the end of this chapter). 1. Go to the MySQL download site and select the link for the current release. 2. In the page that opens, scroll down to find the section marked Windows downloads. There is a separate Windows x64 downloads section for 64-bit versions of Windows. Choose Windows Essentials from the appropriate section, and click the download link. (You may be invited to Pick a mirror instead. This directs you to a mirror site closer to your location, and usually offers a faster download.) 3. Download the MySQL file to your hard disk. It will have a name like mysqlessential-x.x.x-win32.msi, where x.x.x represents the version number. The 64-bit version is called mysql-essential-x.x.x-win64.msi. Make sure you have the correct version. 4. Exit all other Windows programs, and double-click the icon of the file you have just downloaded. This is a self-extracting Windows Installer package. 5. Windows Installer will begin the installation process and open a welcome dialog box. If you are upgrading an existing version of the same series of Windows Essentials to a more recent one, the dialog box will inform you that it has detected your current installation and will remove it before installing the new one. However, all your databases will remain intact. Click Next to continue. 6. Another dialog box may give you the opportunity to change the installation destination. Accept the default and click Next. 7. In the next dialog box, accept the default setup (Typical) and click Next. 8. If you re happy to go ahead with installation, click Install in the next dialog box. 9. Before launching into the actual installation, MySQL invites you to sign up for a free MySQL.com account. I suggest that you select Skip Sign-Up and click Next. After you finish setting everything up, visit www.mysql.com/register.php to see if you re interested in the benefits offered. The main advantage is that you get automatic notification of new versions and links to helpful articles about new features of MySQL. Note: If you are looking for cheap and reliable webhost to host and run your mysql application check mysql web server services.
SETTING UP MYSQL AND PHPMYADMIN phpMyAdmin, which is free and the default method many hosting companies provide for accessing MySQL. This chapter shows you how to install both of them. Which version? MySQL and phpMyAdmin release new versions at a fast-and-furious pace. While this means that new features become available as soon as they re developed, and bugs get squashed quickly, hosting companies rarely keep pace. In fact, many hosting companies are still running the MySQL 3.23 series an excellent database, even though it is two major versions behind the current release. Rather than attempting to find old versions to match the setup on your hosting company, I recommend that you install the latest stable version of both MySQL and phpMyAdmin. That way, you will find it easier to get help if you run into difficulties. Wherever possible, I have designed the code in this book to be backward compatible with MySQL 3.23, and I point out anything that isn t. You can get MySQL from the downloads page at http://dev.mysql.com/downloads. Select the link for the Current Release (Recommended) of MySQL Community Edition Database Server and Client. 10 The installation instructions for MySQL are different for Windows and Mac OS X, so Mac users should skip ahead to the relevant section of the chapter. Because new versions are coming out all the time, I recommend that you check my web- site at http://foundationphp.com/phpsolutions/updates.php before going ahead. Any major updates to the instructions will be listed there. Installing MySQL on Windows MySQL comes in a range of versions, but the one you should choose is Windows Essentials. It contains all the important stuff, and certainly everything you need for this book. If you have a version older than MySQL 4.1.5 already installed on your computer, you must uninstall the old version first. 263 You need excellent and relaible webhost company to host your web applications? Then pay a visit to Inexpensive Web Hosting services.
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY What this chapter covers: Installing the MySQL database on Windows and Mac OS X Securing the database Setting up the phpMyAdmin graphical interface Backing up and transferring data to another server Dynamic websites take on a whole new meaning in combination with a database. Drawing content from a database allows you to present material in ways that would be impractical if not impossible with a static website. Examples that spring to mind are online stores, such as Amazon.com; news sites, such as the International Herald Tribune (www.iht.com); and the big search engines, including Google and Yahoo! Database technology allows these websites to present thousands, sometimes millions, of unique pages with remarkably little underlying code. Even if your ambitions are nowhere near as grandiose, a database can increase your website s richness of content with relatively little effort. First of all, though, you need to choose and install a database system. Why MySQL? Of all the available databases, why choose MySQL? The following reasons should convince you: Cost: The MySQL Community Edition is free under the open source GPL license (www.gnu.org/copyleft/gpl.html). Powerful: The same basic database system as the Community Edition is used by leading organizations such as NASA, Yahoo!, and Alcatel. It s feature-rich and fast. Widespread availability: MySQL is the most popular open source database. Most hosting companies automatically offer MySQL in combination with PHP. Cross-platform compatibility: MySQL runs on Windows, Mac OS X, and Linux. A database created on one system requires no conversion when transferred to another. Open source: Although there is a commercial version, the code and features in the Community Edition are identical. New features are being added constantly. Security: Bugs, when found, are dealt with quickly. So, are there any drawbacks to MySQL? I d love to be able to say it s perfect, but MySQL is not as fully featured as its main commercial rivals, Microsoft SQL Server and Oracle, or its main open source rival, PostgreSQL (www.postgresql.org). However, the missing features are primarily of interest to advanced users, and most are expected to be implemented by MySQL 5.2 (currently, MySQL 5.1 is in an advanced beta stage). Perhaps the biggest drawback for people who don t come from a programming background is the fact that MySQL doesn t come with a glossy interface. That s because MySQL is a database workhorse. It s fast and efficient, and is particularly suited to web-based applications. There are several graphical interfaces available, the most popular being Visit our web design programs services for an affordable and reliable webhost to suit all your needs.
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY The strpos() function takes two arguments: a string that you want to search, and a character or substring that you want to find in it. If it finds the substring that you re looking for, strpos() returns the position of the first character. Since the position of characters in strings is always counted from 0, it s essential to use the identical operator (three equal signs) to make sure that the result of strpos($key, ‘Submit’) is genuinely 0, and not a false negative. To see what I mean, alter the following line: if (strpos($key, ‘Submit’) === 0) { // CORRECT Change it to this: if (strpos($key, ‘Submit’) == 0) { // WRONG If you test the sequence again, you ll see that no results are displayed. This is because strpos() returns false if it can t find the substring; and PHP interprets 0 as false. What you re looking for is not false, but Submit at the beginning of the string in other words, position 0. The identical operator guarantees that the items you re comparing not only have the same value, but are also of the same data type. Don t worry if that last part went over your head. Just remember that when using strpos() to find a character or substring at the start of a string, use the identical oper- ator (===) and not the equality operator (==). The foreach loop on the final page of this example displays the contents of the $_SESSION array onscreen. In a real application, you would use the session variables to build the content of an email message or to prepare a SQL query to insert the information in a database. Coming up . . . If you started this book with little or no knowledge of PHP, you re no longer in the beginners league, but are leveraging the power PHP in a lot of useful ways. Hopefully, by now, you ll have begun to appreciate that the same or similar techniques crop up again and again. Instead of just copying code, you should start to recognize techniques that you can adapt to your needs and experiment on your own. The rest of this book continues to build on your knowledge, but brings a new factor into play: the MySQL relational database, which will take your PHP skills to a higher level. The next chapter shows you how to install MySQL and get it ready for use. Then you ll learn the basics of working with MySQL and a PHP-driven graphical interface called phpMyAdmin before bringing PHP back into the picture. We would like to recommend you tested and proved virtual web hosting services, which you will surely find to be of great quality.
PAGES THAT REMEMBER: SIMPLE LOGIN AND MULTIPAGE FORMS 3. Try clicking the Next button in multiple01.php. The form tells you that the name field is missing. Enter anything, and click Next again. The second form asks for Age. Click Next, and the form takes you without complaint to the third page, which asks for Address. This page won t let you get away with entering nothing. Enter anything, and click Send details. You should see something like the following screenshot: Rather than go through the code in detail, I ll leave you to read the inline comments of the four pages. The first three pages use techniques from the rest of this chapter, combined with the form processing techniques from Chapter 5. The main code in the final page looks like this:
The details submitted were as follows:
$value) { // skip the submit buttons // use identity operator with strpos to prevent false negatives if (strpos($key, ‘Submit’) === 0) { continue; } echo “
$key: $value
“; } // clear the $_SESSION array and destroy session $_SESSION = array(); session_destroy(); ?>
It cleans up the $_SESSION array by removing $_SESSION[’formStarted’] and session variables created by each of the submit buttons. The name and value of the submit button is always part of the $_POST array, so each submit button is added to $_SESSION array as you progress through the forms. You can skip them in the foreach loop by using strpos() to test for the string Submit. Use the same technique for any other session variables that you want to exclude. 257 We highly recommend you visit web and email hosting services if you need stable and cheap web hosting platform for your web applications.
PHP SOLUTIONS: DYNAMIC WEB DESIGN MADE EASY Passing information through multipage forms Variables passed through the $_POST and $_GET arrays have only a fleeting existence. Once they have been passed to a page, they re gone, unless you save their values in some way. The usual method of preserving information that s passed from one form to another is to extract its value from the $_POST array and store it in a hidden field in XHTML like this: J2ee Web Hosting services.
PAGES THAT REMEMBER: SIMPLE LOGIN AND MULTIPAGE FORMS The inline comments explain what is going on; and you should recognize most of the elseif clause from PHP Solution 9-5. PHP measures time in seconds, and I ve set $timelimit (in line 5) to a ridiculously short 15 seconds purely for demonstra tion purposes. To set a more reasonable limit of, say, 15 minutes, change this later like this: $timelimit = 15 * 60; // 15 minutes You could, of course, set $timelimit to 900, but why bother when PHP can do the hard work for you? If the sum of $_SESSION[’start’] plus $timelimit is less than the current time (stored as $now), you end the session and redirect the user to the login page. The line that performs the redirect adds a query string to the end of the URL like this: http://localhost/phpsolutions/sessions/login.php?expired=yes The code in step 2 takes no notice of the value of expired; adding yes as the value just makes it look user-friendlier in the browser address bar. If the script gets as far as the final else, it means that $_SESSION[’authenticated’] has been set, and that the time limit hasn t been reached, so $_SESSION[’start’] is updated to the current time, and the page displays as normal. 4. Copy the code in the preceding step, and use it to replace the code above the DOCTYPE declaration in secretpage.php. 5. Save all three pages, and load either menu.php or secretpage.php into a browser. If the page displays, click Log out. Then log back in, and navigate back and forth between menu.php and secretpage.php. Once you have verified that the links work, wait 15 seconds or more, and try to navigate back to the other page. You should be automatically logged out and presented with the following screen: The code in step 2 is quite long, and is identical for every page that requires it, so it s an ideal candidate for turning into an include. That way, you need update only one script if you decide to change the time limit or the redirect page. The start_session() command can also go in the include file as long as it comes before the use of any session variables. 255 We recommend cheap and reliable webhost to host and run your web applications: Coldfusion Web Hosting services.