Wisdom of MY Words

Random Musings & Book Reviews

Archive for November, 2016

22 November
0Comments

PHP/MySQL Chapter Two

Installation
Developing Locally
Developing your construct on your local computer is the recommended way to learn since you can interact with all of the components on your own machine and not risk causing problems on a production server. That way, if there are problems in the local environment you can fix the snafu’s immediately without having them exposed to your site’s visitors. Advantages to working with your files locally means that you don’t have to FTP them to a server, you don’t have to be connected to the Internet, and you know exactly what’s installed since you did it yourself.

There are three components of installation:
Apache
PHP
MySQL

The bullets indicate the order in which you will install the programs. All our examples will be from a PC install perspective with liner notes on Macintosh installation.

Installing Apache
First Apache needs to be installed and operational before PHP and MySQL can be installed or else they won’t work correctly. Plus, there wouldn’t be any use for the coding application ad database without the Apache web server. A web server delivers web pages, has an IP address and possibly a domain name. For example, if you enter http://www.oreilly.com/ in your browser, this sends a request to the server whose domain name is oreilly.com. The server fetches the page named index.html and sends it to your browser.

Any computer can be turned into a Web server by installing server software and connecting the machine to the Internet, which is why we need to install Apache. All our installation examples are done on a Windows-based PC.
1. Download the Apache 2.0.5 Win32 Binary. The file is downloadable from http://httpd.apache.or/download.cgi and the file that you save to your desktop is apache2_0.55-win32-x86-no_ssl.msi.
If you are on Mac OS X, you already have Apache installed. Open up System Preferences, select the Sharing panel and click to activate Personal Web Sharing (which is actually Apache). Mac OS X 10.2, 10.3, and 10.4 all come with different versions of Apache, but each works perfectly fine.

Figure 2-1. The Installation Wizard prompts you for basic configuration
2. Install Apache using the Installation Wizard. Double click the MSI installer file on your desktop.
The Installation Wizard walks you through the installation process.

Figure 2-2. Apache License Terms and Conditions for Use
3. Accept the License Terms by clicking the radio button. Click Next>.

Figure 2-3. Apache HTTP Server information
4. Click Next >. Additionally, this window offers a number of excellent resources related to the web server.

Figure 2-4. Black Server Information dialog box
5. In the blank dialog box enter all pertinent network information, a sample is provided below. Then click Next >.

Figure 2-5. Sample Server Information
Port 80 is the default HTTP port. In other words, when you request http://www.oreilly.com, you’re implicitly requesting port 80. By accepting this port, your web requests can be made without specifying a non-default port.

Figure 2-6. Selecting a Setup Type
6. Select the Setup Type. Click Next >. The typical install will work for our purposes.

Figure 2-7. Destination Folder dialog box for the Apache installation files
7. Accept the default installation directory. Click Next >.
The default installation directory, C:\Program Files\Apache Group\, is both standard and easy to find especially when you need to make changes to your configuration.

Figure 2-8. Ready to Install dialog box
8. Click Install. The installer installs a variety of modules, you will see some DOS windows appear and disappear. Click Finish when the installer is done.
9. Test your installation by entering http://localhost/ in your browser’s location field. The local host is IP address 127.0.0.1.

Figure 2-9. Apache’s default index page after installation
10. After entering the URL in your browser, the default Apache page displays, which is similar to Figure 2-9. Notice the circled part of the window where it indicates that the installation was successful.
Now that you can serve up web pages, you’re ready to add PHP.

Installing PHP
Go to http://www.php.net/downloads.php to download the latest version of PHP, both binaries and source code can be found on this website. You need to download the zip file versus the Windows installer because the Windows installer only configures the IIS web server.
Create a directory for PHP on your desktop. You don’t need all the files provided in the archive, so unzip the PHP archive into another temporary location.

A temporary location could be on your desktop, which will be easier to find when you go looking for the files.
1. Copy the following files into C:\php: php.exe, php4ts.dll and php2apache2.dll. The php2apache2.dll file is located in the sapi directory.
Use php2apache.dll instead of php2apache2.dll if you are using Apache 1.3.x.
2. Load PHP extensions. If you plan to load PHP extensions, you will need to copy the files in the extensions directory, or the ext directory for PHP 5.x, to C:\php. Or a sub-folder in C:\php, any subfolder as long as the PHP.ini file is updated to reflect the appropriate directory.
MySQL support is now integrated into PHP and doesn’t need an extension dll. However, PHP 5 supports MySQL as a separate module for download. Follow the instructions below to make sure that your installation of PHP works before you start experimenting with installing extensions.
3. Configuring PHP
Copy php.ini-dist from the extracted files into your Windows directory, typically C:\winnt or C:\Windows and rename it to be php.ini.
4. Setup PHP to load Apache as a module. To configure Apache to load PHP as a module to parse your PHP scripts, use a text editor to open the Apache configuration file, httpd.conf, typically found in C:\Program Files\Apache Group\Apache2\conf\.

Figure 2-10. Viewing httpd.conf in Notepad
5. Loading PHP as an Apache module. Open the httpd.conf file in Notepad or another text editor and search for the section that has a series of commented out LoadModule statements. You can find this around line 134. In Notepad >Edit > Go To…> 134.
Statements prefixed by the hash “#” sign in HTML and PHP are considered commented out and can only be seen by you, never your end user in their browser window.

Figure 2-11. Notepad httpd.conf file with the LoadModule section highlighted
6. Restart the Apache server by selecting Start Menu | Apache HTTP Server 2.0.x | Control Apache Server | Restart, so that it can read the new configuration directives you placed into httpd.conf. Alternatively, in the system tray, double click on the Apache icon and click the Restart button.

Figure 2-12. Notepad httpd.conf file with the AddType section highlighted
7. Search for AddType in the file, around line 754, and add the following line after the last AddType statement: AddType application/x-httpd-php .php
Testing Your PHP Installation
1. Create a PHP file, in any text editor, with the following line:
2. Save the file as phpinfo.php and save it under the Apache htdocs directory, usually C:\Program Files\Apache Group\Apache2\htdocs.
3. Open your browser of choice.
Access the file you just created by typing localhost/phpinfo.php into your browser’s location bar. You should see a page of information about your PHP setup like Figure 2-13.

Figure 2-13. PHP Introduction screen
If for some reason it doesn’t work, check to see whether your PHP or Apache setup is causing the problem by the command line prompt: C:\php, then type in php “C:\Program Files\Apache Group\Apache2\htdocs\phpinfo.php”, since the file isn’t in the c:\php directory.
If invoking PHP from the command line causes a large HTML file with all the PHP configuration information displayed, then your PHP set up is fine. The problem is probably your Apache configuration, go ahead and re-read the procedure on how to install and configure Apache.

Enabling PHP on Mac OS X
If you are on Mac OS X you have PHP pre-installed on your computer. You need to edit the Apache configuration file to enable PHP in much the same way you edited the PC text file.

Installing MySQL 4.1
Rounding out the last component that allows you to develop and test pages on your local computer is MySQL. Now download the MySQL Installer.
1. Download the MySQL binaries.
Both the binaries and the source code can be found at http://dev.mysql.com/downloads/. The link takes you to a page where you can enter personal info, or just click No Thanks to download the file. A number of download locations (or mirrors) are available, select one. Download the recommended version for the Windows Essentials (x86) MySQL 4.1 installer. Save the installer file to your desktop.
2. Installing MySQL using the Installation Wizard

Figure 2-14. The Installation Wizard prompts you for configuration settings
Double click the MSI installer file on your desktop. A setup wizard walks you through the installation process. If, perchance you’re installing MySQL 5.0 the install process is the same as it is for MySQL 4.1.
3. The Installation Wizard displays. Click Next >.

Figure 2-15. Select a setup type
4. Select Typical Installation by clicking the Typical radio button, then click Next >.

Figure 2-16. Confirm Installation Folder
5. Click Install on the Ready to Install the Program dialog box.

Figure 2-17. The MySQL.com account provides newsletters and bug reporting resources
6. Select Skip Sign-Up and click Next >. You can optionally sign up for an account, it provides access to a monthly newsletter as well as the ability to post bugs and comments on the online forums.

Figure 2-18. Configure the MySQL server
7. Select the Configure the MySQL Server now check box, then click Finish >.

Figure 2-19. The Configuration Wizard customizes the database settings
8. On the MySQL Server Instance Configuration Wizard click Click Next >.

Figure 2-20. Chose the level of detail dialog box
9. Select the Standard Configuration radio button from Figure 2-20. Click Next >.

Figure 2-21. How to Launch MySQL window
10. Check both Install As Window Service and Launch the MySQL Server automatically check boxes selected. Click Next >.

Figure 2-22. Security settings for the database window
11. Enter a password for the root user in the password and confirm fields. Click Next >. You do not need the Anonymous Account since you can do everything with named accounts

Figure 2-23. Commit the configuration settings window
12. Click Execute as shown in Figure 2-23.

Figure 2-24. Figure Caption Text Goes Here
13. Click Finish so that MySQL is now configured and running on your computer.
All critical components: Apache, PHP 5.0 and MySQL 4.1 are installed. The wizard will inform you of basic problem during installation like running out of free disk space or not having proper permissions on your system to install MySQL.
Working Remotely
Although we recommend you start out working locally you can use an ISP account as long as it supports PHP and MySQL.
You’ll need login information to the remote server and you may need to use your ISP’s web-based tool to create your database.
In order to transfer your files and directories you’ll need to activate a File Transfer Protocol (FTP) account at your ISP, usually through your account control panel. Once you have an FTP login you upload your HTML and PHP files using a FTP client.
While your computer likely has the command line version of the FTP client available from the command prompt it can be cryptic to use. Graphical FTP clients make using FTP much easier. FTP Voyager, available from http://sourceforge.net/projects/filezilla/ is one FTP client you can use to upload files to your ISP. Your initial login screen will look similar to Figure 2-25. Fetch is a good FTP program for the Macintosh.

Figure 2-25. FTP Voyager initial screen
After connecting you’ll see a dialog similar to Figure 2-26, but you will not see the identical screen as this FTP Voyager screen. You can drag and drop the .php file you created. Remember, in order for your PHP file to run you need to save it with an extension of .php instead of .html, otherwise it won’t run, because the web server needs to know it’s a PHP file in order to run the PHP interpreter.

Figure 2-26. FTP Voyager directory listing
PHP files must be accessed through a web server since your web browser doesn’t have the ability to interpret the PHP code. A PHP interpreter is used to process the PHP files.
You’re ready to rock and roll and start learning all about basic facts, integration and how to get your dynamic web page up and running as quickly and smoothly as possible. In the next chapter we’ll give you basic information about PHP and simple coding principles that apply to using PHP.

21 November
0Comments

PHP/MySQL Preface

Learning PHP & mySQL
By Michele E Davis and Jon A Phillips

Preface
For many users they become comfortable with creating their own static web sites either coding the HTML directly or using a web development tool such as Dreamweaver. The next step is usually making your pages come alive by customizing their appearance per visitor and remember visitor details.

The combination of PHP, Apache, and MySQL pose many advantages to users that are new to building dynamic pages. They are all free open source products. Additionally, there’s an enormous amount of code freely available on the Internet already for PHP and MySQL. If you can think it up there’s a good chance someone has already done it and you can leverage their knowledge. Nobody likes reinventing the wheel.

Our goal is to help you learn the ins and outs of PHP and MySQL and save you some of the “Why doesn’t that work?” moments that we’ve already been through. PHP and MySQL both continue to evolve based on feedback from the community. Most of the time this involves adding new features but sometimes it also means reacting to common pitfalls from real words feedback. For example, the usage of global variables has been restricted to reduce common security lapses. While this eliminates a common security hole it also broke a substantial amount of code available on the web. We’ll show you what to look for and how to fix this problem without pulling out your hair.
Who should read this book

This book is for graphic designers, English and History college majors who find themselves in an IT firm creating static web sites and need to move forward with coding and database drive web sites. Or people involved in a small startup who easily know flash development and HTML markup but need to expand their repertoire of skills to database and open source coding dynamic coding environments.

Who should not read this book
If you’re not interested in using a database with your dynamic web sites than this book isn’t the best choice for you. If you already now how to create pages using MySQL and PHP than you’d also be better of with a book that is more a reference than a learning book such as the nutshell series. If you’re not familiar with HTML markup you should learn that first since you’re using PHP to generate HTML output.

Organization of this book
This book starts out with an overview of how all of the pieces we’ll be working with fit together. Because there are multiple languages and technologies that interact to form the dynamic web pages it’s best to start with a solid foundation of how the pieces work together. The PHP that you’ll learn works as a sort of glue or duct-tape to hold all the pieces together.

Next we’ll walk through installing the core software packages on your local computer. These include the Apache web server. The PHP interpreter works with the web server when processing dynamic content. Finally, we’ll install the MySQL database. Installation is covered on the PC, Mac, and Linux systems. You can also use a hosted ISP account to develop your pages if you don’t want to install everything locally.
Since PHP plays an important role in pulling everything together we take the time to explain the basics of working with the PHP language. This includes language essentials such as data types, program flow logic, and variables. Functions, arrays, and forms each get their own chapter to fully explore them.
Since you may be new to database in general we ease into MySQL by first explaining concepts that apply to designing and using any relation database. Then we give specific examples of using MySQL to interact with your data. Once you can get data in and out of the database you’ll need to work with PHP to integrate that data into your dynamic content.

Security and access control to your web pages each get their own chapter. While security may sounds like a dull subject it is still a huge issue if you store any information that’s private on your web page. There are several common security pitfalls that we’ll guide you around.
Finally, we close with sample applications that demonstrate how the technologies work together to rapidly build workable, fast, web sites. You’ll also learn where to look for additional information on the topics covered in the book.

Supporting Books
Even if you feel you are ready for this book, you may want to explore some of the XML technologies in greater depth than is possible here. The following lists offer some good places to start.
Tony Steidler-Dennison, Run Your Own Web Server Using Linux & Apache (SitePoint, 2005.)
Paul Hudson, PHP in a Nutshell, First Edition. (O’Reilly & Associates, 2005.)
Russell Dyer, MySQL in a Nutshell, First Edition. (O’Reilly & Associates, 2005.)
CSS Cookbook, Christopher Schmitt, First Edition. (O’Reilly & Associates, 2004.)
There are also several good resources for dynamic web development including onlamp.com, part of the O’Reilly Network. LAMP stands for Linux, Apache, MySQL, PHP. LAMP is the defacto standard for serving dynamic web pages.

Conventions Used in this Book
The following font conventions are used in this book:
Italic is used for:
• Pathnames, filenames, and program names
• Internet addresses, such as domain names and URLs
• New items where they are defined
Constant Width is used for:
• Command lines and options that should be typed verbatim
• Names and keywords in programs, including method names, variable names, and class names
• XML element tags
Constant-Width Bold is used for emphasis in program code lines.
Constant-Width Italic is used to indicate replaceable arguments within program code.

How to Contact Us
We have tested and verified the information in this book to the best of our ability, but you may find that features have changed (or even that we have made a few mistakes!) Please let us know about any errors you find, as well as your suggestions for future editions, by writing to:
O’Reilly & Associates, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
1-800-998-9938 (in the U.S. or Canada)
1-707-829-0515 (international/local)
1-707-829-0104 (fax)
You can also send us messages electronically. To be put on the mailing list or request a catalog, send email to:
info@oreilly.com
To ask technical questions or comment on the book, send email to:
bookquestions@oreilly.com
We have a web site for the book, where we’ll list examples, errata, and any plans for future editions. You can access this page at:
http://www.oreilly.com/catalog/officeXMLess
Note to production: we need to verify that this is the correct URL for the book.
[what’s the URL for our book]
For more information abut this book and others, see the O’Reilly web site:
http://www.oreilly.com/
Acknowledgements
First we’d like to thank our wonderful agent, Matt Wagner of Fresh Books for bringing such a fabulous opportunity to the table for us. The opportunity to write for O’Reilly & Associates was a great honor, as technologists, our bookcase was already well populated with a lot of animals from the Nutshell series.
Thanks to our O’Reilly developmental editors, Brett McLaughlin and Simon St. Laurent. This book wouldn’t be what it is without both of their help working with us. Secondly, profuse thanks to our technical editors; especially Jereme Allen and Christopher Finke who we met through our local Minneapolis/St. Paul PHP community: http://www.tcphp.org. Technical edit thanks also go to our last technical editor, Patrick Krekelberg.