1. Trang chủ
  2. » Công Nghệ Thông Tin

PHP and MySQL Web Development - P79 pptx

5 178 0
Tài liệu đã được kiểm tra trùng lặp

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

THÔNG TIN TÀI LIỆU

Thông tin cơ bản

Định dạng
Số trang 5
Dung lượng 112,65 KB

Các công cụ chuyển đổi và chỉnh sửa cho tài liệu này

Nội dung

362 Chapter 17 Using Network and Protocol FunctionsListing 17.2 directory_submit.html—HTML for the Submission Form Submit your site Submit site URL: Email contact: This is a ve

Trang 1

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

Trang 2

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);

Trang 3

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 fragment:anchor Listing 17.3 Continued

Trang 4

In our script, we only want the hostinformation, 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

falseif 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 trueif 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

Trang 5

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 configureprogram with the

enable-ftpoption 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 />";

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

TỪ KHÓA LIÊN QUAN