PHP and MySQL Web Development - P79 pptx

5 177 0
PHP and MySQL Web Development - P79 pptx

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

Thông tin tài liệu

362 Chapter 17 Using Network and Protocol Functions Listing 17.2 directory_submit.html—HTML for the Submission Form <head> <title>Submit your site</title> </head> <body> <h1>Submit site</h1> <form method="post" action="directory_submit.php"> URL: <input type="text" name="url" size="30" value="http://"><br /> Email contact: <input type="text" name="email" size="23"><br /> <input type="submit" name="Submit site"> </form> </body> </html> This is a very simple form—the rendered version, with some sample data entered, is shown in Figure 17.2. Figure 17.2 Directory submissions typically require your URL and some contact details so directory administrators can notify you when your site is added to the directory. When the submit button is pressed, we want to check, first, that the URL is hosted on a real machine, and, second, that the host part of the email address is also on a real machine.We have written a script to check these things, and the output is shown in Figure 17.3. The script that performs these checks uses two functions from the PHP network functions suite—gethostbyname() and getmxrr().The full script is shown in Listing 17.3. 22 525x ch17 1/24/03 3:40 PM Page 362 363 Using Network Lookup Functions Figure 17.3 This version of the script displays the results of checking the hostnames for the URL and email address—a production version might not display these results, but it is interesting to see the information returned from our checks. Listing 17.3 directory_submit.php—Script to Verify URL and Email Address <html> <head> <title>Site submission results</title> </head> <body> <h1>Site submission results</h1> <?php // Extract form fields $url = $HTTP_POST_VARS['url']; $email = $HTTP_POST_VARS['email']; // Check the URL $url = parse_url($url); $host = $url['host']; if(!($ip = gethostbyname($host))) { echo 'Host for URL does not have valid IP'; exit; } echo "Host is at IP $ip <br />"; // Check the email address $email = explode('@', $email); 22 525x ch17 1/24/03 3:40 PM Page 363 364 Chapter 17 Using Network and Protocol Functions $emailhost = $email[1]; // note that the getmxrr() function is *not implemented* in // Windows versions of PHP if (!getmxrr($emailhost, $mxhostsarr)) { echo 'Email address is not at valid host'; exit; } echo 'Email is delivered via: '; foreach ($mxhostsarr as $mx) echo "$mx "; // If reached here, all ok echo '<br />All submitted details are ok.<br />'; echo 'Thank you for submitting your site.<br />' .'It will be visited by one of our staff members soon.' // In real case, add to db of waiting sites ?> </body> </html> Let’s go through the interesting parts of this script. First, we take the URL and apply the parse_url() function to it.This function returns an associative array of the different parts of a URL.The available pieces of infor- mation are the scheme, user, pass, host, port, path, query, and fragment.Typically, you aren’t going to need all of these, but here’s an example of how they make up a URL. Given a URL such as http://nobody:secret@bigcompany.com:80/script.php?variable=value#anchor the values of each of the parts of the array would be n scheme: http:// n user: nobody n pass: secret n host: bigcompany.com n port: 80 n path: script.php n query: variable=value n fragment: anchor Listing 17.3 Continued 22 525x ch17 1/24/03 3:40 PM Page 364 365 Using FTP In our script, we only want the host information, so we pull it out of the array as fol- lows: $url = parse_url($url); $host = $url['host']; After we’ve done this, we can get the IP address of that host, if it is in the DNS.We can do this using the gethostbyname() function, which will return the IP if there is one, or false if not: $ip = gethostbyname($host); You can also go the other way using the gethostbyaddr() function, which takes an IP as parameter and returns the hostname. If you call these functions in succession, you might well end up with a different hostname from the one you began with.This can mean that a site is using a virtual hosting service. If the URL is valid, we then go on to check the email address. First, we split it into username and hostname with a call to explode(): $email = explode('@', $email); $emailhost = $email[1]; When we have the host part of the address, we can check to see if there is a place for that mail to go using the getmxrr() function: getmxrr($emailhost, $mxhostsarr); This function returns the set of MX (Mail Exchange) records for an address in the array you supply at $mxhostarr. An MX record is stored at the DNS and is looked up like a hostname.The machine listed in the MX record isn’t necessarily the machine where the email will eventually end up. Instead it’s a machine that knows where to route that email. (There can be more than one, hence this function returns an array rather than a hostname string.) If we don’t have an MX record in the DNS, there’s nowhere for the mail to go. Note that the getmxrr() function is not implemented in Windows versions of PHP. If all these checks are okay, we can put this form data in a database for later review by a staff member. In addition to the functions we’ve just used, you can use the more generic function checkdnsrr(), which takes a hostname and returns true if there is any record of it in the DNS. Using FTP File Transfer Protocol, or FTP, is used to transfer files between hosts on a network. Using PHP, you can use fopen() and the various file functions with FTP as you can with HTTP connections, to connect to and transfer files to and from an FTP server. However, there is also a set of FTP-specific functions that comes with the standard PHP install. 22 525x ch17 1/24/03 3:40 PM Page 365 366 Chapter 17 Using Network and Protocol Functions These functions are not built in to the standard install by default. In order to use them under UNIX, you will need to run the PHP configure program with the enable-ftp option and then rerun make. If you are using the standard Windows install, FTP functions are enabled auto- matically. (For more details on configuring PHP, see Appendix A,“Installing PHP 4 and MySQL.”) Using FTP to Back Up or Mirror a File The FTP functions are useful for moving and copying files from and to other hosts. One common use you might make of this is to back up your Web site or mirror files at another location.We will look at a simple example using the FTP functions to mirror a file.This script is shown in Listing 17.4. Listing 17.4 ftpmirror.php—Script to Download New Versions of a File from an FTP Server <html> <head> <title>Mirror update</title> </head> <body> <h1>Mirror update</h1> <?php // set up variables - change these to suit application $host = 'ftp.cs.rmit.edu.au'; $user = 'anonymous'; $password = 'laura@tangledweb.com.au'; $remotefile = '/pub/tsg/teraterm/ttssh14.zip'; $localfile = '/tmp/writable/ttssh14.zip'; // connect to host $conn = ftp_connect("$host"); if (!$conn) { echo 'Error: Could not connect to ftp server<br />'; exit; } echo "Connected to $host.<br />"; // log in to host @ $result = ftp_login($conn, $user, $pass); if (!$result) { echo "Error: Could not log on as $user<br />"; 22 525x ch17 1/24/03 3:40 PM Page 366 . option and then rerun make. If you are using the standard Windows install, FTP functions are enabled auto- matically. (For more details on configuring PHP, see Appendix A,“Installing PHP 4 and MySQL. ”) Using. with HTTP connections, to connect to and transfer files to and from an FTP server. However, there is also a set of FTP-specific functions that comes with the standard PHP install. 22 525x ch17 1/24/03. Network and Protocol Functions These functions are not built in to the standard install by default. In order to use them under UNIX, you will need to run the PHP configure program with the enable-ftp

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

Từ khóa liên quan

Mục lục

  • PHP and MySQL Web Development

  • Copyright

  • Table of Contents

  • Introduction

  • Part I: Using PHP

    • Chapter 1: PHP Crash Course

    • Chapter 2: Storing and Retrieving Data

    • Chapter 3: Using Arrays

    • Chapter 4: String Manipulation and Regular Expressions

    • Chapter 5: Reusing Code and Writing Functions

    • Chapter 6: Object-Oriented PHP

    • Part II: Using MySQL

      • Chapter 7: Designing Your Web Database

      • Chapter 8: Creating Your Web Database

      • Chapter 9: Working with Your MySQL Database

      • Chapter 10: Accessing Your MySQL Database from the Web with PHP

      • Chapter 11: Advanced MySQL

      • Part III: E-commerce and Security

        • Chapter 12: Running an E-commerce Site

        • Chapter 13: E-commerce Security Issues

        • Chapter 14: Implementing Authentication with PHP and MySQL

        • Chapter 15: Implementing Secure Transactions with PHP and MySQL

        • Part IV: Advanced PHP Techniques

          • Chapter 16: Interacting with the File System and the Server

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

Tài liệu liên quan