home *** CD-ROM | disk | FTP | other *** search
/ H4CK3R 4 / hacker04 / 04_HACK04.ISO / src / PHP / tidbits1.txt < prev    next >
Encoding:
Text File  |  2002-05-06  |  4.2 KB  |  115 lines

  1. PHP: network adminstrator's best friend 
  2.  
  3. As a web-developer, you're probably used to such lovely tools as ping, whois, nslookup etc. But what when you need one of those utilities at a client's office and have no access to telnet? Good guess. Time to look up the functions in the "Network" section of the PHP manual. 
  4.  
  5. Socket operations
  6. -----------------
  7.  
  8. The most important function there is fsockopen(). Using this function, you can connect to any open port on a server and establish a socket connection with it. The function's syntax is as following:
  9. int fsockopen(string hostname, int port, int [errno], string [errstr]);
  10.  
  11. The first two arguments are obvious, the next two are optional and used for error handling. "errno" and "errstr" should be passed by reference. "Passing by reference" means that the original variable will get modified. Normally, the content of a variable passed to a function wouldn't be modified. 
  12.  
  13. So, you could use this function to open a connection to a webserver and print out the headers: 
  14.  
  15. $fp = fsockopen ("phpwizard.net", 80, &$errnr, &$errstr) or die("$errno: $errstr");
  16. fputs($fp,"GET / HTTP/1.0\n");
  17. while (!$end)
  18. {
  19. $line = fgets($fp, 2048);
  20. if (trim($line) == "")
  21. $end = true;
  22. else
  23. echo $line;
  24. }
  25. fclose($fp); 
  26.  
  27. In this example you see that you can apply any file operations (fread, fwrite etc) to the the pointer you got using the fsockopen() call. Note that the example realizes a HTTP/1.0 client - it won't work with name-based virtual hosts. 
  28.  
  29. Finger
  30. ------
  31.  
  32. Naturally, you can also open connections to other ports. Writing a small finger client with PHP is trivial therefore. Let's change the example from above to query a finger daemon: 
  33.  
  34. $fp = fsockopen("phpwizard.net", 79, &$errno, &$errstr) or die("$errno: $errstr");
  35. fputs($fp, "root\n");
  36. while (!feof($fp))
  37. echo fgets($fp, 128);
  38. fclose($fp); 
  39.  
  40.  
  41.  
  42. Easy, isn't it?
  43.  
  44. Whois 
  45.  
  46. Querying a whois server uses the same concept: 
  47.  
  48. $fp = fsockopen ("whois.internic.net", 43, &$errnr, &$errstr) or die("$errno: $errstr");
  49. fputs($fp, "phpwizard.net\n");
  50. while (!feof($fp))
  51. echo fgets($fp, 2048);
  52. fclose($fp); 
  53.  
  54.  
  55. Blocking and non-blocking operations
  56. ------------------------------------
  57.  
  58. But there's a problem with all those functions. They work fine if a) you have a conenction with low latency and b) if the server you're connecting to is up and running. If not, your script will be busy until it times out. The reason for this is that default socket connections are blocking and don't time out. You can avoid these "hanging scripts" by switching to non-blocking socket operations. The function set_socket_blocking() does just that: it set all operations on a socket (first parameter: socket pointer) to either blocking (second parameter: true) or false (second parameter: false). Using non-blocking operations, the finger function would like like this: 
  59.  
  60. $fp = fsockopen($host, 79, &$errno, &$errstr) or die("$errno: $errstr");
  61. set_socket_blocking($fp, 0);
  62. fputs($fp, "$user\n");
  63.  
  64. $stop = time() + $timeout;
  65. while (!feof($fp) && time()<$stop)
  66. echo fgets($fp, 128);
  67. fclose($fp);
  68.  
  69.  
  70. For your convenience, I've made the three examples from above into own functions. Modifying them to use non-blocking socket calls is left as an exercise for you. If you do it, I'd be happy to make it available on phpWizard.net. function 
  71.  
  72. finger ($host, $user)
  73. {
  74. $fp = fsockopen($host, 79, &$errno, &$errstr) or die("$errno: $errstr");
  75. fputs($fp, "$user\n");
  76. while (!feof($fp))
  77. echo fgets($fp, 128);
  78. fclose($fp); 
  79. }
  80.  
  81. function whois($domain, $server="whois.internic.net")
  82. $fp = fsockopen ($server, 43, &$errnr, &$errstr) or die("$errno: $errstr");
  83. fputs($fp, "$domain\n");
  84. while (!feof($fp))
  85. echo fgets($fp, 2048);
  86. fclose($fp); 
  87.  
  88. function get_headers($host, $path = "/")
  89. {
  90. $fp = fsockopen ("$host", 80, &$errnr, &$errstr) or die("$errno: $errstr");
  91. fputs($fp,"GET $path HTTP/1.0\n\n");
  92. while (!$end)
  93. {
  94. $line = fgets($fp, 2048);
  95. if (trim($line) == "")
  96. $end = true;
  97. else
  98. echo $line;
  99. }
  100. fclose($fp); 
  101.  
  102.  
  103. Notification: 
  104.  
  105. Send an email with "subscribe" in the body to phpTidbits-request@htmlwizard.net. 
  106.  
  107.  
  108.  
  109. Full issue in HTML-format: 
  110.  
  111. Send an email with "subscribe" in the body to phpTidbits-html-request@htmlwizard.net. 
  112.