PHP/File Directory/fsockopen

Материал из Web эксперт
Перейти к: навигация, поиск

fsockopen() function establishes a socket connection, TCP or UDP.

   <source lang="html4strict">

Its syntax is: int fsockopen (string host, int port [, int errnumber [, string errstring [, int timeout]]]) errnumber and errstring return error information specific to the attempt to connect to the host. <? function get_the_host($host,$path) {

    $fp = fsockopen($host, 80, &$errno, &$errstr, 30);
    socket_set_blocking($fp, 1);
    fputs($fp,"GET $path HTTP/1.1\r\n");
    fputs($fp,"Host: $host\r\n\r\n");
    $x = 1;
    while($x < 10) :
         $headers = fgets($fp, 4096);
         print $headers;
         $x++;
    endwhile;
    fclose($fp);

} get_the_host("www.wbex.ru", "/"); ?>

 </source>
   
  


fsockopen.php

   <source lang="html4strict">

<?php

  $http = fsockopen("www.example.ru",80);
  $req = "GET / HTTP/1.1\r\n";
  $req .= "Host: www.example.ru\r\n";
  $req .= "Connection: Close\r\n\r\n";
  fputs($http, $req);
  while(!feof($http))
  {
     echo fgets($http, 1024);
  }
  fclose($http);

?>

 </source>
   
  


Outputting the Status Lines Returned by Web Servers

   <source lang="html4strict">

<html> <head> <title>Outputting Server Status Lines</title> </head> <body>

<?php $to_check = array (

         "www.google.ru" => "/index.html",
         "www.html.ru"    => "/notthere.html",
         "www.java.ru"   => "/nohost.html"
     );

foreach ( $to_check as $host => $page ) {

print "

"; $fp = @fsockopen( "$host", 80, $errno, $errdesc, 10); print "Trying $host
"; if ( ! $fp ) { print "Couldn"t connect to $host:
"; print "Error: $errno
"; print "Desc: $errdesc
"; } else { print "Trying to get $page
"; fputs( $fp, "HEAD $page HTTP/1.0" ); fputs( $fp, "Host: $host" ); fputs( $fp, "\r\n" ); print fgets( $fp, 1024 ); fclose( $fp ); } print "

\n";

} ?>

</body> </html>

 </source>
   
  


Retrieving a Web Page Using fsockopen()

   <source lang="html4strict">

<html> <head> <title>Retrieving a Web page using fsockopen()</title> </head> <body>

<?php $host = "www.wbex.ru"; $page = "/index.htm"; $fp = fsockopen( "$host", 80, $errno, $errdesc ); if ( ! $fp ) {

 die ( "Couldn"t connect to $host:\nError: $errno\nDesc: $errdesc\n" );

} $request = "GET $page HTTP/1.0\r\n"; $request .= "Host: $host\r\n"; $request .= "Referer: http://www.wbex.ru/page.html\r\n"; $request .= "User-Agent: PHP test client\r\n\r\n"; $page = array(); fputs ( $fp, $request ); while ( ! feof( $fp ) ) {

 $page[] = fgets( $fp, 1024 );

} fclose( $fp ); print "the server returned ".(count($page))." lines!"; ?>

</body> </html>

 </source>
   
  


Scan a server

   <source lang="html4strict">

<?php

  ini_set("max_execution_time", 120);
  $rangeStart = 0;
  $rangeStop = 1024;
  $target = "www.example.ru";
  
  $range =range($rangeStart, $rangeStop);
echo "

Scan results for $target

";
  foreach ($range as $port) {
     $result = @fsockopen($target, $port,$errno,$errstr,1);
if ($result) echo "

Socket open at port $port

";
  }

?>

 </source>
   
  


Sockets Are Files

   <source lang="html4strict">

<?

   $fp = fsockopen ("data.org", 80);
   if ($fp) {
           fwrite($fp, "GET / HTTP/1.1\r\nHOST: data.org\r\n\r\n");
           while (!feof($fp)) {
                   print fread($fp,256);
           }
           fclose ($fp);
   } else {
           print "Fatal error\n";
   }

?>

 </source>
   
  


SSL Streams

   <source lang="html4strict">

<?php $conn = @fsockopen("tcp://www.example.ru", 80); If (!$conn) die("Unable to connect to www.example.ru"); fwrite($conn, "GET / HTTP/1.0\r\n"); fwrite($conn, "Host: www.example.ru\r\n\r\n"); fpassthru($conn); ?>

 </source>