PHP 5/MySQL Programming- P7 potx

5 1.6K 0
PHP 5/MySQL Programming- P7 potx

Đang tải... (xem toàn văn)

Thông tin tài liệu

While you’re getting started, I recommend not allowing external access. To make a new home page for your computer, look for a directory called htdocs under your Apache installation. Apache is configured to automatically display a file called index.html if the file exists. On live servers, I usually have an index.html page so the user gets a nice HTML page when she goes to a particular directory. However, for my own devel- opment server, I usually take out the index page so I can see a directory listing and navigate the htdocs directory through the server. Starting Apache as a Service You can run Apache as an executable program, but it’s preferable to start it as a service. Services are background processes that automatically restart whenever the computer is restarted. Services don’t usually have a graphic interface, but they sometimes have icons in the task bar. To run Apache as a service, activate services from the control panel (on my machine the path is Control Panel/Administrative Tools/Services). Figure 1.3 TRICK TRAP 8 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r FIGURE 1.2 The default page for Apache proves a local server is running. 9 C h a p t e r 1 E x p l o r i n g t h e P H P E n v i r o n m e n t shows the services control panel. Use this panel to turn your various services on or off. Note that if you change your server’s configuration, you must turn it off, then back on before your server recognizes the changes. The newest versions of Apache seem to launch themselves as services automati- cally. If this happens, great. Don’t worry about the DOS window I mentioned; it won’t be there. The most important thing is whether you get a page when your browser is pointed at localhost. If so, you have a functioning Web server. Configuring Apache Apache is configured through a series of heavily commented text files. Look in the conf directory of your Apache directory for a file called httpd.conf. This is the main configuration file for Apache. You shouldn’t have to change this file much, but this is the file to modify if you want, for example, to add a domain name. After installing PHP, change httpd.conf to tell Apache where it can find PHP. Stay tuned—I show you how to do that once PHP is installed. Running Your Local Server The Apache directory has an htdocs subdirectory. Any files you want displayed on your local server must be in this directory or its subdirectories. TRICK FIGURE 1.3 This control panel starts and stops Apache. 10 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r You might normally double-click a file in your file manager to display it in a browser, or you may drag it to the browser from your file-management system. This works for plain HTML files, but it bypasses the local server. That means PHP programs will not work correctly. PHP code must be called through a formal http call, even if it’s localhost. All PHP code will be in an htdocs directory’s subdirectory, unless you specifically indicate in your httpd.conf file that you want another directory to be accessible to your Web server. Installing PHP The PHP environment is a series of programs and library files. These programs are unusual because the user never runs them directly. Instead, a user requests a PHP program from a Web server and the server calls upon PHP to process the instruc- tions in the file. PHP then returns HTML code, which the user sees in the browser. Downloading the PHP Program The examples in this book use PHP 5.0, which is available on the accompanying CD. You can also go to http://www.php.net to get the PHP Windows binaries. You can install PHP wherever you wish, but I installed it to an Apache subdirectory so all my PHP programs are in proximity. 1. See install.txt in the PHP directory. This is a very important document. Be sure to look at it carefully and follow its instructions. 2. Find the numerous .dll files in the PHP directory. 3. Make sure the .dll files are in the same directory as PHP.exe. When you tell Apache how to find PHP, it will also find these important files. TRAP IS THIS APPROPRIATE FOR BEGINNERS? To tell you the truth, I think installation of Apache, PHP, and MySQL is a big headache. It isn’t easy to get right. It’s best if you can find a way to skip all this stuff and begin programming on a working server. If you cannot rely on some- body else to set up the server for you, the rest of the chapter will guide you through the process. I’m sorry that you have to start with a really messy process. Even if you have access to a server that supports PHP, it’s not a bad idea to look through the rest of this chapter. You need to know how to check the con- figuration as well as how to change it (if you’re allowed). 4. Find a file called php.ini-recommended. 5. Copy it to your C:\windows directory. 6. Rename the new file php.ini. Later on you edit this file to configure Apache, but you need to install PHP first. The install.txt document suggests the php.ini file goes in C:\winnt. I found that worked fine with PHP version 4, but version 5 requires the file to be in C:\windows (at least, that was the case on my machine). If your configurations are not taking hold, check this file’s location. You should also be able to put the file in your Apache directory—but if you do, that’s the only version you should have. If you get strange behavior, check to see that you don’t have an extra copy of php.ini floating around somewhere. Later in this chapter, I show you how to change this file so the programs contained in this book run without problems. For now, though, be sure that PHP is running. Telling Apache about PHP 1. Open the Apache configuration file in your text editor. Remember, this file is called httpd.conf and it’s probably in the conf directory of your Apache installation. 2. Find a section containing a series of loadmodule directives. If you’re using PHP version 5, you must specifically tell Apache where to find it. 3. After all the other loadmodule commands, add the following code: LoadModule php5_module c:/apache/php5apache.dll 4. Modify the code so it points to wherever the php5apache.dll file was installed in your system. 5. Scroll down until you see a series of AddModule commands. 6. Add the following code to httpd.conf to add the module: AddModule mod_php5.c 7. Add the following line to the end of the file: AddType application/x-httpd-php .php 8. Save httpd.conf and restart Apache to ensure the changes are permanent. TRAP 11 C h a p t e r 1 E x p l o r i n g t h e P H P E n v i r o n m e n t 12 P H P 5 /M y S Q L P r o g r a m m i n g f o r t h e A b s o l u t e B e g i n n e r Adding PHP to Your Pages Now that you’ve got PHP installed, it’s time to add some code. See that PHP is installed and run a quick diagnostic check to see how it is con- figured. You should do this whether you’re installing your own Web server or using an existing server for your programs. The easiest way to determine if PHP exists on your server is this: Write a simple PHP program and see if it works. Here’s a very simple PHP program that greets the user and displays all kinds of useful information about the development system. Adding PHP Commands to an HTML Page <html> <head> <title>Hello in PHP</title> </head> <body> <h1>Hello in PHP</h1> <? print “Hello, world!”; phpInfo(); ?> </body> </html> Since this is the first PHP code you’ve seen in this book, I need to go over some basic concepts. A page written in PHP begins much like an ordinary HTML page. Both are writ- ten with a plain text editor and stored on a Web server. What makes a PHP pro- gram different is the embedded <script> elements. When the user requests a PHP page, the server examines the page and executes any script elements before sending the resulting HTML to the user. The <? ?> sequence is the easiest way to indicate PHP code, but it isn’t always the best way. You can also indicate PHP code with a longer version, like this: <?php ?>. This version works better when your code is interpreted as XML. You can also specify your code with normal HTML tags much like JavaScript: <script language = “php”></script>. Some PHP servers are configured to prefer one type of script tag over another so you may need to be flexible. However, all these variations work in exactly the same way. TRICK . calls upon PHP to process the instruc- tions in the file. PHP then returns HTML code, which the user sees in the browser. Downloading the PHP Program The examples in this book use PHP 5.0, which. You can also go to http://www .php. net to get the PHP Windows binaries. You can install PHP wherever you wish, but I installed it to an Apache subdirectory so all my PHP programs are in proximity. 1 system. Adding PHP Commands to an HTML Page <html> <head> <title>Hello in PHP& lt;/title> </head> <body> <h1>Hello in PHP& lt;/h1> <? print “Hello, world!”; phpInfo(); ?> </body> </html> Since

Ngày đăng: 07/07/2014, 02:20

Từ khóa liên quan

Mục lục

  • PHP 5 / MySQL Programming for the Absolute Beginner

    • Cover

    • Contents

    • Introduction

    • Chapter 1: Exploring the PHP Environment

    • Chapter 2: Using Variables and Input

    • Chapter 3: Controlling Your Code with Conditions and Functions

    • Chapter 4: Loops and Arrays

    • Chapter 5: Better Arrays and String Handling

    • Chapter 6: Working with Files

    • Chapter 7: Writing Programs with Objects

    • Chapter 8: XML and Content Management Systems

    • Chapter 9: Using MySQL to Create Databases

    • Chapter 10: Connecting to Databases within PHP

    • Chapter 11: Data Normalization

    • Chapter 12: Building a Three-Tiered Data Application

    • Index

    • Team DDU

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan