Beginning PHP and MySQL From Novice to Professional phần 5 pot

108 315 0
Beginning PHP and MySQL From Novice to Professional phần 5 pot

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

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

398 CHAPTER 15 ■ HANDLING FILE UPLOADS [size] => 22616 [type] => text/plain [error] => ) The key values and their respective properties are discussed earlier in this chapter, so there’s no reason to describe them again (besides, all the names are rather self- explanatory). If you’re interested in just retrieving the value of a single property, pass a key to the getProp() call. For example, suppose you want to know the size (in bytes) of the file: echo $files->getProp('size'); This produces the following output: 22616 Uploading Multiple Files One of the beautiful aspects of HTTP_Upload is its ability to manage multiple file uploads. To handle a form consisting of multiple files, all you have to do is invoke a new instance of the class and call getFiles() for each upload control. Suppose the aforementioned professor has gone totally mad and now demands five homework assignments daily from his students. The form might look like this: <form action="multiplehomework.php" enctype="multipart/form-data" method="post"> Last Name:<br /> <input type="text" name="name" value="" /><br /> Homework #1:<br /> <input type="file" name="homework1" value="" /><br /> Homework #2:<br /> <input type="file" name="homework2" value="" /><br /> Homework #3:<br /> <input type="file" name="homework3" value="" /><br /> Homework #4:<br /> <input type="file" name="homework4" value="" /><br /> Homework #5:<br /> <input type="file" name="homework5" value="" /><br /> <p><input type="submit" name="submit" value="Submit Notes" /></p> </form> Gilmore_862-8C15.fm Page 398 Wednesday, February 13, 2008 7:08 AM CHAPTER 15 ■ HANDLING FILE UPLOADS 399 Handling this with HTTP_Upload is trivial: $homework = new HTTP_Upload(); $hw1 = $homework->getFiles('homework1'); $hw2 = $homework->getFiles('homework2'); $hw3 = $homework->getFiles('homework3'); $hw4 = $homework->getFiles('homework4'); $hw5 = $homework->getFiles('homework5'); At this point, simply use methods such as isValid() and moveTo() to do what you will with the files. Summary Transferring files via the Web eliminates a great many inconveniences otherwise posed by firewalls and FTP servers and clients. It also enhances an application’s ability to easily manipulate and publish nontraditional files. In this chapter, you learned just how easy it is to add such capabilities to your PHP applications. In addition to offering a comprehensive overview of PHP’s file-upload features, several practical examples were discussed. The next chapter introduces in great detail the highly useful Web development topic of tracking users via session handling. Gilmore_862-8C15.fm Page 399 Wednesday, February 13, 2008 7:08 AM Gilmore_862-8C15.fm Page 400 Wednesday, February 13, 2008 7:08 AM 401 ■ ■ ■ CHAPTER 16 Networking You may have turned to this chapter wondering just what PHP could possibly have to offer in regard to networking. After all, aren’t networking tasks largely relegated to languages commonly used for system administration, such as Perl or Python? While such a stereotype might have once painted a fairly accurate picture, these days, incorpo- rating networking capabilities into a Web application is commonplace. In fact, Web- based applications are regularly used to monitor and even maintain network infra- structures. Furthermore, with the introduction of the command-line interface (CLI) in PHP version 4.2.0, PHP is now increasingly used for system administration among developers who wish to continue using their favorite language for other purposes. The PHP developers, always keen to acknowledge growing needs in the realm of Web appli- cation development and to remedy demands by incorporating new features into the language, have put together a rather amazing array of network-specific functionality. This chapter is divided into sections covering the following topics: DNS, servers, and services: PHP offers a variety of functions capable of retrieving information about the network internals, DNS, protocols, and Internet addressing schemes. This section introduces these functions and offers several usage examples. Sending e-mail with PHP: Sending e-mail via a Web application is undoubtedly one of the most commonplace features you can find these days, and for good reason. E-mail remains the Internet’s killer application and offers an amazingly efficient means for communicating and maintaining important data and information. This section explains how to easily send messages via a PHP script. Additionally, you’ll learn how to use the PEAR packages Mail and Mail_Mime to facilitate more complex e-mail dispatches, such as those involving multiple recipients, HTML formatting, and the inclusion of attachments. Gilmore_862-8C16.fm Page 401 Thursday, February 14, 2008 6:46 AM 402 CHAPTER 16 ■ NETWORKING Common networking tasks: In this section, you’ll learn how to use PHP to mimic the tasks commonly carried out by command-line tools, including pinging a network address, tracing a network connection, scanning a server’s open ports, and more. DNS, Services, and Servers These days, investigating or troubleshooting a network issue often involves gathering a variety of information pertinent to affected clients, servers, and network internals such as protocols, domain name resolution, and IP addressing schemes. PHP offers a number of functions for retrieving a bevy of information about each subject, each of which is introduced in this section. ■Note Several of the functions introduced in this chapter don’t work on Windows. Check out the PEAR package Net_DNS to emulate their capabilities. DNS The Domain Name System (DNS) is what allows you to use domain names (e.g., example.com) in place of the corresponding not-so-user-friendly IP address, such as 192.0.34.166. The domain names and their complementary IP addresses are stored and made available for reference on domain name servers, which are interspersed across the globe. Typically, a domain has several types of records associated to it, one mapping the IP address to the domain, another for directing e-mail, and another for a domain name alias, for example. Often network administrators and developers require a means to learn more about various DNS records for a given domain. This section introduces a number of standard PHP functions capable of digging up a great deal of information regarding DNS records. Checking for the Existence of DNS Records The checkdnsrr() function checks for the existence of DNS records. Its prototype follows: int checkdnsrr(string host [, string type]) Gilmore_862-8C16.fm Page 402 Thursday, February 14, 2008 6:46 AM CHAPTER 16 ■ NETWORKING 403 DNS records are checked based on the supplied host value and optional DNS resource record type, returning TRUE if any records are located, and FALSE otherwise. Possible record types include the following: A: IPv4 Address Record. Responsible for the hostname-to-IPv4 address translation. AAAA: IPv6 Address Record. Responsible for the hostname-to-IPv6 address translation. A6: IPv6 Address Record. Used to represent IPv6 addresses. Intended to supplant present use of AAAA records for IPv6 mappings. ANY: Looks for any type of record. CNAME: Canonical Name Record. Maps an alias to the real domain name. MX: Mail Exchange Record. Determines the name and relative preference of a mail server for the host. This is the default setting. NAPTR: Naming Authority Pointer. Allows for non-DNS-compliant names, resolving them to new domains using regular expression rewrite rules. For example, an NAPTR might be used to maintain legacy (pre-DNS) services. NS: Name Server Record. Determines the name server for the host. PTR: Pointer Record. Maps an IP address to a host. SOA: Start of Authority Record. Sets global parameters for the host. SRV: Services Record. Denotes the location of various services for the supplied domain. Consider an example. Suppose you want to verify whether the domain name example.com has a corresponding DNS record: <?php $recordexists = checkdnsrr("example.com", "ANY"); if ($recordexists) echo "The domain name has been reserved. Sorry!"; else echo "The domain name is available!"; ?> This returns the following: The domain name has been reserved. Sorry! Gilmore_862-8C16.fm Page 403 Thursday, February 14, 2008 6:46 AM 404 CHAPTER 16 ■ NETWORKING You can also use this function to verify the existence of a domain of a supplied mail address: <?php $email = "ceo@example.com"; $domain = explode("@",$email); $valid = checkdnsrr($domain[1], "ANY"); if($valid) echo "The domain exists!"; else echo "Cannot locate MX record for $domain[1]!"; ?> This returns the following: The domain exists! Keep in mind this isn’t a request for verification of the existence of an MX record. Sometimes network administrators employ other configuration methods to allow for mail resolution without using MX records (because MX records are not mandatory). To err on the side of caution, just check for the existence of the domain, without specifically requesting verification of whether an MX record exists. Further, this doesn’t verify whether an e-mail address actually exists. The only definitive way to make this determination is to send that user an e-mail and ask him to verify the address by clicking a one-time URL. You can learn more about one-time URLs in Chapter 14. Retrieving DNS Resource Records The dns_get_record() function returns an array consisting of various DNS resource records pertinent to a specific domain. Its prototype follows: array dns_get_record(string hostname [, int type [, array &authns, array &addtl]]) Although by default dns_get_record() returns all records it can find specific to the supplied domain (hostname), you can streamline the retrieval process by specifying a type, the name of which must be prefaced with DNS. This function supports all the types introduced along with checkdnsrr(), in addition to others that will be introduced in a moment. Finally, if you’re looking for a full-blown description of this hostname’s DNS Gilmore_862-8C16.fm Page 404 Thursday, February 14, 2008 6:46 AM CHAPTER 16 ■ NETWORKING 405 description, you can pass the authns and addtl parameters in by reference, which specify that information pertinent to the authoritative name servers and additional records also should be returned. Assuming that the supplied hostname is valid and exists, a call to dns_get_record() returns at least four attributes: host: Specifies the name of the DNS namespace to which all other attributes correspond. class: Returns records of class Internet only, so this attribute always reads IN. type: Determines the record type. Depending upon the returned type, other attributes might also be made available. ttl: Calculates the record’s original time-to-live minus the amount of time that has passed since the authoritative name server was queried. In addition to the types introduced in the section on checkdnsrr(), the following domain record types are made available to dns_get_record(): DNS_ALL: Retrieves all available records, even those that might not be recognized when using the recognition capabilities of your particular operating system. Use this when you want to be absolutely sure that all available records have been retrieved. DNS_ANY: Retrieves all records recognized by your particular operating system. DNS_HINFO: Specifies the operating system and computer type of the host. Keep in mind that this information is not required. DNS_NS: Determines whether the name server is the authoritative answer for the given domain, or whether this responsibility is ultimately delegated to another server. Just remember that the type names must always be prefaced with DNS_. As an example, suppose you want to learn more about the example.com domain: <?php $result = dns_get_record("example.com"); print_r($result); ?> Gilmore_862-8C16.fm Page 405 Thursday, February 14, 2008 6:46 AM 406 CHAPTER 16 ■ NETWORKING A sampling of the returned information follows: Array ( [0] => Array ( [host] => example.com [type] => NS [target] => a.iana-servers.net [class] => IN [ttl] => 110275 ) [1] => Array ( [host] => example.com [type] => A [ip] => 192.0.34.166 [class] => IN [ttl] => 88674 ) ) If you were only interested in the name server records, you could execute the following: <?php $result = dns_get_record("example.com","DNS_CNAME"); print_r($result); ?> This returns the following: Array ( [0] => Array ( [host] => example.com [type] => NS [target] => a.iana-servers.net [class] => IN [ttl] => 21564 ) [1] => Array ( [host] => example.com [type] => NS [target] => b.iana-servers.net [class] => IN [ttl] => 21564 ) ) getmxrr() Gilmore_862-8C16.fm Page 406 Thursday, February 14, 2008 6:46 AM [...]... ttl= 255 time= 158 usec 64 bytes from www.example.com (192.0.34.166): icmp_seq=1 ttl= 255 time =57 usec 64 bytes from www.example.com (192.0.34.166): icmp_seq=2 ttl= 255 time =58 usec - www.example.com ping statistics 5 packets transmitted, 3 packets received, 0% packet loss round-trip min/avg/max/mdev = 0.048/0.078/0. 158 /0.041 ms PHP s program execution functions are great because they allow you to take... supplied to ldap_bind() are created and managed within the LDAP server and have nothing to do with any accounts residing on the server or the workstation from which you are connecting Therefore, if you are unable to connect anonymously to the LDAP server, you need to talk to the system administrator to arrange for an appropriate account Also, demonstrated in the previous example, to connect to the test... directory services products depend heavily upon an open specification known as the Lightweight Directory Access Protocol, or LDAP In this chapter, you will learn how easy it is to talk to LDAP via PHP s LDAP extension In the end, you’ll possess the knowledge necessary to begin talking to directory services via your PHP applications Because an introductory section on LDAP wouldn’t be nearly enough to. .. showing you just how easy it is to interact with your preferred directory server using PHP s LDAP extension Gilmore_862-8C17.fm Page 4 25 Thursday, February 14, 2008 6 :51 AM CHAPTER 17 ■■■ PHP and LDAP As corporate hardware and software infrastructures expanded throughout the last decade, IT professionals found themselves overwhelmed with the administrative overhead required to manage the rapidly growing... Mail and Mail_Mime To take advantage of Mail and Mail_Mime, you’ll first need to install both packages To do so, invoke PEAR and pass along the following arguments: 413 Gilmore_862-8C16.fm Page 414 Thursday, February 14, 2008 6:46 AM 414 CHAPTER 16 ■ NE TWORKING %>pear install Mail Mail_Mime Execute this command and you’ll see output similar to the following: Starting to download Mail-1.1.13.tgz (17 ,52 7... 192.168.1.101 as the IP address and 255 . 255 . 255 .0 as the subnet mask, you should see the output shown in Figure 16-2 Figure 16-2 Calculating network addressing Testing User Bandwidth Although various forms of bandwidth-intensive media are commonly used on today’s Web sites, keep in mind that not all users have the convenience of a high-speed network connection at their disposal You can automatically test a user’s... resources being added to the enterprise Printers, workstations, servers, switches, and other miscellaneous network devices all required continuous monitoring and management, as did user resource access and network privileges Quite often the system administrators cobbled together their own internal modus operandi for maintaining order, systems that all too often were poorly designed, insecure, and nonscalable... popular products: • Fedora Directory Server: http://directory.fedoraproject.org/ • Microsoft Active Directory: http://www.microsoft.com/activedirectory/ • Novell eDirectory: http://www.novell.com/products/edirectory/ • Oracle Collaboration Suite: http://www.oracle.com/collabsuite/ 4 25 Gilmore_862-8C17.fm Page 426 Thursday, February 14, 2008 6 :51 AM 426 CHAPTER 17 ■ PHP AND LDAP Figure 17-1 A model of... its own part to manage some of the enterprise, yet coming at a cost of considerable overhead because of the lack of integration The result was that both users and administrators suffered from the absence of a comprehensive management solution, at least until directory services came along Directory services offer system administrators, developers, and end users alike a consistent, efficient, and secure... to killall This is necessary because the command executed by the system call will continue to execute if the user ends the process prematurely Because ending execution of the script within the browser will not actually stop the process for execution on the server, you need to do it manually Sample output follows: PING www.example.com (192.0.34.166) from 123. 456 .7.8 : 56 (84) bytes of data 64 bytes from . The PHP developers, always keen to acknowledge growing needs in the realm of Web appli- cation development and to remedy demands by incorporating new features into the language, have put together. divided into sections covering the following topics: DNS, servers, and services: PHP offers a variety of functions capable of retrieving information about the network internals, DNS, protocols, and. learn how to use PHP to mimic the tasks commonly carried out by command-line tools, including pinging a network address, tracing a network connection, scanning a server’s open ports, and more. DNS,

Ngày đăng: 09/08/2014, 14:21

Từ khóa liên quan

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

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

Tài liệu liên quan