{"id":1317,"date":"2018-01-19T19:22:42","date_gmt":"2018-01-19T19:22:42","guid":{"rendered":"http:\/\/goofy-trucks.flywheelsites.com\/php-scripts-for-interacting-with-networks\/"},"modified":"2018-01-19T19:24:41","modified_gmt":"2018-01-19T19:24:41","slug":"php-scripts-for-interacting-with-networks","status":"publish","type":"post","link":"https:\/\/phpbuilder.com\/php-scripts-for-interacting-with-networks\/","title":{"rendered":"PHP Scripts for Interacting with Networks"},"content":{"rendered":"<div class=\"phpbuilder-content\">\n<div class=\"phpbuilder-meta\">\n<div class=\"\">By Leidago Noabeb<\/div>\n<div class=\"\">on December 14, 2010<\/div>\n<\/p><\/div>\n<div id=\"overflow-content\">\n<div class=\"articlePara\">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. <a href=\"NetworkingSourceCode.html\">Click here<\/a> to download the accompanying source code.<\/div>\n<h2>Accessing Other Websites with PHP<\/h2>\n<div class=\"articlePara\">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.<\/div>\n<div class=\"articlePara\">In PHP, you access a website in pretty much the same way that you would access a text file on your hard drive &#8212; by using <code>fopen()<\/code>.<\/div>\n<div class=\"example\"><code><\/p>\n<pre>fopen(<em>http:\/\/localhost\/websecure\/ftp.php<\/em>, \"r\");<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">The <code>fopen()<\/code> 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 <code>fopen()<\/code> method has the modes listed in the following table.<\/div>\n<div class=\"articlePara\"><strong>Table 1. Modes of <em>fopen()<\/em> Method<\/strong><\/p>\n<table width=\"50%\" border=\"1\" cellspacing=\"1\" cellpadding=\"1\">\n<tr>\n<th>Mode<\/th>\n<th>Meaning<\/th>\n<\/tr>\n<tr>\n<td>r<\/td>\n<td>Reading only &#8212; Begins reading at the start of the file<\/td>\n<\/tr>\n<tr>\n<td>r+<\/td>\n<td>Reading or writing &#8212; Begins reading at the start of the file<\/td>\n<\/tr>\n<tr>\n<td>w<\/td>\n<td>Writing only &#8212; Creates the file if it does not exist, and overwrites any existing contents<\/td>\n<\/tr>\n<tr>\n<td>w+<\/td>\n<td>Reading or writing &#8212; Creates the file if it does not exist, and overwrites any existing contents (when writing)<\/td>\n<\/tr>\n<tr>\n<td>a<\/td>\n<td>Writing only &#8212; Creates the file if it does not exist, and appends the new data to the end of the file<\/td>\n<\/tr>\n<tr>\n<td>a+<\/td>\n<td>Reading or writing &#8212; Creates the file if it does not exist, and overwrites any existing contents (when writing)<\/td>\n<\/tr>\n<tr>\n<td>x<\/td>\n<td>Writing only &#8212; Creates the file if it does not exist, but does nothing (issues a warning) when the file does exist<\/td>\n<\/tr>\n<tr>\n<td>x+<\/td>\n<td>Reading or writing &#8212; Creates the file if it does not exist, but does nothing (issues a warning) when the file does exist<\/td>\n<\/tr>\n<\/table>\n<\/div>\n<div class=\"articlePara\">You 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 <code>fopen<\/code> does not support redirects. For example, this example is fine:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>fopen(\"http:\/\/localhost\/websecure\/ftp.php\/\", \"r\");<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">But this one will fail:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>fopen(http:\/\/localhost\/websecure\/ftp.php, \"r\");<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">When you&#8217;ve opened a file, you can pretty much treat it the same way that you would any other file, using common functions such as <code>file()<\/code> or <code>fgets()<\/code> to place or retrieve the data.<\/div>\n<div class=\"articlePara\">Example code might be something like this:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>&lt;!DOCTYPE html PUBLIC \"-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\" <br\/>    \"http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\"&gt;\n&lt;html xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\"&gt;\n&lt;head&gt;\n&lt;meta http-equiv=\"Content-Type\" content=\"text\/html; charset=iso-8859-1\" \/&gt;\n&lt;title&gt;Untitled Document&lt;\/title&gt;\n&lt;\/head&gt;\n\n&lt;body&gt;\n&lt;form action=\"tester.php\" method=\"get\"&gt;\n&lt;input name=\"url\" type=\"text\" \/&gt;\n&lt;input name=\"\" type=\"button\" \/&gt;\n&lt;\/form&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">And the processing code might look something like this:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>&lt;?\n$url=$_POST['url'];\n$fp=file($url);\n$n=count($fp);\n$r=rand(0,($n-1));\necho trim($fp[$r]);\necho $n;\n?&gt;<\/pre>\n<p><\/code><\/div>\n<h2>DNS and PHP<\/h2>\n<div class=\"articlePara\">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):<\/div>\n<div class=\"example\"><code><\/p>\n<pre>gethostbyname()\n    string gethostbyname(string hostname)<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">This function takes a single argument &#8212; the hostname of a machine &#8212; and returns a string corresponding to the IP address:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>    &lt;?php \n    $host = \"localhost\"; \n    $ip = gethostbyname($host);\n\n    echo \"The IP address of $host is $ip\"; \n    ?&gt;<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">This script displays the IP address of <em>http:\/\/localhost<\/em> 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.<\/div>\n<div class=\"example\"><code><\/p>\n<pre>gethostbynamel()\n\n    array gethostbynamel(string hostname)<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">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, <code>gethostbynamel()<\/code> works like <code>gethostbyname()<\/code> but returns the complete list of IP addresses associated with that hostname as an array:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>    &lt;?php \n    $host= \"localhost\"; \n    $ip= gethostbynamel($host);\n\n    echo(\"The IP addresses of $hostare:&lt;br&gt;n\"); \n    for ($i = 0; $i &lt; count($ip); $i++) {\n        echo(\"$ip [i] &lt;br&gt;n\"); \n    }\n    ?&gt;<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">Assuming that <em>web.local.com<\/em> is a machine with multiple IP addresses, this script prints a list of all those addresses.<\/div>\n<div class=\"articlePara\">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 <a href=\"http:\/\/www.isc.org\/products\/BIND\/\" target=\"newFrame\">here<\/a>.<\/div>\n<div class=\"example\"><code><\/p>\n<pre>gethostbyaddr()\n    string gethostbyaddr(string ip_address)<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">This function does the reverse of <code>gethostbyname()<\/code> in that, given the IP address as an argument, it returns the host name corresponding to it.<\/div>\n<div class=\"example\"><code><\/p>\n<pre>    &lt;?php \n    $ip = \"127.0.0.1\"; \n    $host= gethostbyaddr($ip);\n\n    echo(\"The host name corresponding to the IP\n          address $ip is $host\"); \n    ?&gt;<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">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).<\/div>\n<div class=\"articlePara\">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 <code>ip_address<\/code> argument itself. In fact, if an error occurs, the return value of the function is the <code>ip_address<\/code> argument.<\/div>\n<div class=\"example\"><code><\/p>\n<pre>getprotobyname()\n\n    int getprotobyname(string name)<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">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 <em>\/etc\/protocols<\/em> on UNIX-derived systems and <em>%SystemRoot%System32driversetcprotocol<\/em> on Microsoft Windows NT or Windows 2000.<\/div>\n<div class=\"example\"><code><\/p>\n<pre>getprotobynumber()\n    string getprotobynumber(int number)<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">This function has just the opposite functionality of the <code>getprotobyname()<\/code> function in that, given the protocol number as an argument, it displays the protocol name.<\/div>\n<div class=\"example\"><code><\/p>\n<pre>getservbyname()\n    int getservbyname(string service, string protocol)<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">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 <em>\/etc\/services<\/em> file:<\/div>\n<div class=\"example\"><code><\/p>\n<pre>    &lt;?php \n    $protocol = \"smtp\"; \n    $portNum = getservbyname($protocol, \"tcp\");\n\n    echo(\"The port number of the $protocol service is $portNum\"); \n    ?&gt;<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">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 <em>\/etc\/protocol<\/em> file.<\/div>\n<div class=\"example\"><code><\/p>\n<pre>getservbyport()\n    string getservbyport(int port, string protocol)<\/pre>\n<p><\/code><\/div>\n<div class=\"articlePara\">This function has just the opposite functionality of <code>getservbyname()<\/code>; 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.<\/div>\n<div class=\"articlePara\">As we saw, the <code>getprotobyname()<\/code>, <code>getprotobynumber()<\/code>, <code>getservbyname()<\/code>, and <code>getservbynumber()<\/code> 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.<\/div>\n<\/div>\n<p><\/p>\n<div style=\"float: left; padding:15px; color:#17AAF3\">\n<div style=\"background-color:#B6E5FC; font-size:16px; margin-top:1px; padding:1px 4px 1px 4px; color:#000; font-style:bold; float:left;\">1<\/div>\n<div style=\"float:left; font-size:16px; color:#FF7A22; padding:2px 2px 2px 2px; \">| <\/div>\n<div style=\"float:left; padding:2px 4px 2px 4px;\"><a class=\"pageNumber\" href=\"Leidago_Noabeb121420104658.html?page=2\">2<\/a> <\/div>\n<div style=\"float:left; padding:2px;\"><a class=\"paginationPageLink\" href=\"Leidago_Noabeb121420104658.html?page=2\">Next Page \u00bb<\/a><\/div>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Use these PHP tools and functions to make your PHP scripts more useful in a network<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1317","post","type-post","status-publish","format-standard","hentry","category-tutorials"],"_links":{"self":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1317","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/comments?post=1317"}],"version-history":[{"count":1,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1317\/revisions"}],"predecessor-version":[{"id":3205,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/posts\/1317\/revisions\/3205"}],"wp:attachment":[{"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/media?parent=1317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/categories?post=1317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/phpbuilder.com\/wp-json\/wp\/v2\/tags?post=1317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}