Our Recommendation

Showing posts with label generator. Show all posts
Showing posts with label generator. Show all posts

Wednesday, December 15, 2010

PHP scripts to interact with (PHP generator) networks

PHP Scripts for Interacting with Networks

by Leidago Noabeb

PHP has a great many tools for interacting with a network and also with the Internet. In this article, I examine some of those tools and functions to show how exactly you can use them to make your scripts more useful in a network environment. Click here to download the accompanying source code.Accessing other sites with PHP is extremely easy, but why would you want to do that? If you just want to get information from a website (for instance, a website that offers weather reports for a city that is of interest to you), then you can write a spider to get that information and more.In PHP, you access a website in pretty much the same way that you would access a text file on your hard drive -- by using fopen().fopen(http://localhost/websecure/ftp.php, "r");The fopen() function that you use to open files is the same one you use to open Web pages, because a Web page is essentially a file on a server. The fopen() method has the modes listed in the following table.Table 1. Modes of fopen() Method
Reading only -- Begins reading at the start of the fileReading or writing -- Begins reading at the start of the fileWriting only -- Creates the file if it does not exist, and overwrites any existing contentsReading or writing -- Creates the file if it does not exist, and overwrites any existing contents (when writing)Writing only -- Creates the file if it does not exist, and appends the new data to the end of the fileReading or writing -- Creates the file if it does not exist, and overwrites any existing contents (when writing)Writing only -- Creates the file if it does not exist, but does nothing (issues a warning) when the file does existReading or writing -- Creates the file if it does not exist, but does nothing (issues a warning) when the file does existYou will be able to open a file only for reading, unless the permissions are set otherwise. Also, you will have to use a trailing slash after a directory because fopen does not support redirects. For example, this example is fine:fopen("http://localhost/websecure/ftp.php/", "r");But this one will fail:fopen(http://localhost/websecure/ftp.php, "r");When you've opened a file, you can pretty much treat it the same way that you would any other file, using common functions such as file() or fgets() to place or retrieve the data.Example code might be something like this:"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
And the processing code might look something like this:PHP provides a powerful set of functions for DNS name resolution. The functions that PHP provides are limited to DNS client functionality; it provides no server-related functions. However, the DNS client functions are enough because most applications will require only a name-resolution service. Below is a list of some of the functions that are available in PHP (from the PHP manual):gethostbyname() string gethostbyname(string hostname)This function takes a single argument -- the hostname of a machine -- and returns a string corresponding to the IP address: This script displays the IP address of http://localhost as a dot-separated string of numbers. We need to obtain the IP address of a host before we can connect to it using the sockets API, as you shall soon see.gethostbynamel() array gethostbynamel(string hostname)Machines running operating systems that support virtual IP addresses (that is, one network card can have multiple IP addresses) can have more than one IP address associated with them. In this case, gethostbynamel() works like gethostbyname() but returns the complete list of IP addresses associated with that hostname as an array: \n"); for ($i = 0; $i < count($ip); $i++) { echo("$ip [i]
\n"); } ?>Assuming that web.local.com is a machine with multiple IP addresses, this script prints a list of all those addresses.The other scenario where one DNS name maps on to multiple IP addresses is when certain DNS server implementations (such as BIND) support a feature known as DNS round robin. This is a rudimentary load-balancing mechanism where one DNS name maps to multiple machines (that is, IP addresses of multiple machines). The DNS queries are resolved by the server to each of these IP addresses using a round robin scheme, thereby distributing the request load between multiple machines. Find more information on BIND here.gethostbyaddr() string gethostbyaddr(string ip_address)This function does the reverse of gethostbyname() in that, given the IP address as an argument, it returns the host name corresponding to it. This script displays the hostname corresponding to the IP address 127.0.0.1, which is always the localhost machine (that is, the machine the script was run on).While each machine on the TCP/IP network must have at least one IP address, there is no requirement that it should have a DNS name. Thus, it is entirely possible that a machine may have an IP address and no DNS entry. In which case, the function returns the ip_address argument itself. In fact, if an error occurs, the return value of the function is the ip_address argument.getprotobyname() int getprotobyname(string name)This function returns the protocol number associated with a TCP/IP protocol, which is a unique integer associated with the protocol name that is passed to it as an argument. The name to protocol mapping is stored in a text file, usually in /etc/protocols on UNIX-derived systems and %SystemRoot%\System32\drivers\etc\protocol on Microsoft Windows NT or Windows 2000.getprotobynumber() string getprotobynumber(int number)This function has just the opposite functionality of the getprotobyname() function in that, given the protocol number as an argument, it displays the protocol name.getservbyname() int getservbyname(string service, string protocol)We need to obtain a well-known port number at which a service is running before our client can connect to it. This function takes a service name and the transport protocol (that is, either TCP or UDP) and returns the port number associated with the service. On most UNIX machines, the port number to service mapping is specified in the /etc/services file: This prints the port number of the Simple Mail Transfer Protocol (SMTP) as 25. On most UNIX machines, the information mapping the protocol name with the protocol number is available in the /etc/protocol file.getservbyport() string getservbyport(int port, string protocol)This function has just the opposite functionality of getservbyname(); it prints the service name associated with the port number argument. The protocol argument indicates the transport protocol (that is TCP or UDP) that the service is implemented over.As we saw, the getprotobyname(), getprotobynumber(), getservbyname(), and getservbynumber() functions obtain their information from files on the local file system (on most platforms). Therefore, they are not really DNS functions, but I examine them in this context because they are essentially resolver functions.[ Next Page ]



You can post questions/corrections/feedback here

 

If you are looking for help, please post on the appropriate forum here. Your questions will be answered much more quickly.


Saturday, December 11, 2010

10 Tips PHP (PHP generator) associative array handling

Associative array - an essential data type used to describe a collection of unique key and associated values - is a pillar of all included PHP programming languages. In fact, associative arrays are so central that PHP supports dozens of functions and other features capable of manipulating data table in every conceivable way Web development tasks. This broad support can be a bit overwhelming for developers who are seeking the most effective way to manipulate charts in their applications. In this article I will offer 10 tips to help you to shred, slice and dice your data in countless ways.1. Adding table ElementsPHP is a weakly typed language which means that you're not forced to explicitly declare an array nor its size. Instead of this you can declare them and populate the table simultaneously: capitals $ = array ('Alabama' = "Montgomery", 'Alaska' = "Juneau", 'Arizona' = "Phoenix"); additional table elements can be added as follows:$ capitals ['Arkansas'] = "Little Rock". If you are dealing with numerically indexed arrays and would rather be preceded and add items to a function named explicitly, check array_push() and array_unshift() functions, these functions do not work with associative arrays). 2 Delete array ElementsTo remove an element of an array, use the unset() function: unset($capitals['California']); using numerically indexed arrays you have somewhat more flexibility in terms of remove items from the array that you can use the functions array_shift() and array_pop() to remove an item from the beginning and end of the array, respectively.3. Exchange ValuesSuppose and key you want to create a new array named $States, which would use the capitals of the State as the index names and status as the associated value. This task is accomplished easily using array_flip() function:$ cities = array ('Alabama' = "Montgomery", 'Alaska' = "Juneau", 'Arizona' = > 'Phoenix');$ State = array_flip($capitals); / / declares $ = array (/ / = string "Alabama", "Montgomery" / / "Juneau" = string "Alaska", / / "Phoenix" = string "Arizona" / /); 4 Merger of ArraysSuppose preceding tables have been used in conjunction with a Web-based "flash card" service and you wish to offer students a way to test their knowledge of the capitals around the world, included States. You can merge tables containing capitals State and country using the array_merge() function:$ stateCapitals = array ('Alabama' = "Montgomery", 'Alaska' = "Juneau", 'Arizona' = > 'Phoenix');$ countryCapitals = array ('Australia' = > 'Canberra', 'Austria' = > 'Vienna', 'Algeria' = "Algiers");$ capitals = array_merge ($ stateCapitals, $countryCapitals); 5 Table ValuesSuppose data found in a table potentially publishing contains errors of capitalization, and you want to correct these errors before to insert data into the database. You can use the array_map() function to implement a callback to each capitalize($element) function: array element {$ element = strtolower($element); ucwords($element); return} capitals $ = array ('Alabama' = "montGoMEry", 'Alaska' = "Juneau", 'Arizona' = > 'Phoenix');$ capitals = array_map ("profit", $capitals);[Next page]



You can post questions/comments/corrections here

 

If you are looking for help, please post to the appropriate here forum. Your questions will be respond much more quickly.