I am trying to getting url in fopen -function, but only output is those error messages.

Warning: php_hostconnect: connect failed in F:\pubwww\html\php\ipv6.php on line 32

Warning: fopen("http://ankka.com:1000/?ip-txt", "r") - Bad file descriptor in F:\public\html\php\ipv6.php on line 32

Warning: fgets(): supplied argument is not a valid File-Handle resource in F:\public\html\php\ipv6.php on line 33

Warning: fclose(): supplied argument is not a valid File-Handle resource in F:\public\html\php\ipv6.php on line 34

PHP code (part that do not work):

 $fp=fopen("http://ankka.com:1000/?ip-txt","r");
 fgets($fp,$clientIP);
 fclose($fp); // this is line 34

That address contains only my public ip address that should be read to $clientIP variable. Is this right way?

What is wrong this part of script? Or is there better solution, example something function like this ( get_public_ip() )?

PS. Sorry about bad english but I am from Finland.

LukeSW

edit: I am using Apache 2.0.46 (or 2.0.49, don't remember) and PHP 4.2.2 on Windows 2000 Pro.

    I update PHP from 4.2.2 to 4.3.3 but still coming those error messages. Messages syntax has changed.

    Warning: fopen(http://ankka.com:1000/?ip-txt): failed to open stream: Bad file descriptor in F:\public\html\php\ipv6.php on line 32

    Warning: fgets(): supplied argument is not a valid stream resource in F:\public\html\php\ipv6.php on line 33

    Warning: fclose(): supplied argument is not a valid stream resource in F:\public\html\php\ipv6.php on line 34

    LukeSW

      If PHP has decided that filename specifies a registered protocol, and that protocol is registered as a network URL, PHP will check to make sure that allow_url_fopen is enabled. If it is switched off, PHP will emit a warning and the fopen call will fail.

        allow_url_fopen is enabled ( = On ) in php.ini file.

        Should I enable either of those below?

        ;always_populate_raw_post_data = On
        ;cgi.rfc2616_headers = 0

        LukeSW

          Thanks for a hint, LordShryku.

          Software firewall was blocking php-connections. Corrected for now.

          But although no errors is showing anymore, the result is wrong.

          Result:


          Private IP:

          IPv4 -address: 192.168.1.2
          IPv6 -address: 0000:0000:0000:0000:0000:0000:C0A8:0102

          Public IP:

          IPv4 -address: 192.168.1.2
          IPv6 -address: 0000:0000:0000:0000:0000:0000:C0A8:0102

          Script returns private ip-address although it should be return public ip-address.

          However, http://ankka.com:1000/?ip-txt address returns public address to client browser when pointing that ip address.

          (Part of PHP code)

           $fp=fopen("http://ankka.com:1000/?ip-txt","r");
           fgets($fp,$clientIP);
           fclose($fp);
          
           if (strlen($clientIP)==0) { $clientIP="0.0.0.0"; }
           $sclientip=$clientIP."";
           $ipv4=explode(".",$sclientip);
           $ipv4_1=$ipv4[0]; $ipv4h_1=strtoupper(dechex($ipv4_1));
           $ipv4_2=$ipv4[1]; $ipv4h_2=strtoupper(dechex($ipv4_2));
           $ipv4_3=$ipv4[2]; $ipv4h_3=strtoupper(dechex($ipv4_3));
           $ipv4_4=$ipv4[3]; $ipv4h_4=strtoupper(dechex($ipv4_4));
           if (strlen($ipv4h_1)<2) { $ipv4h_1="0".$ipv4h_1; }
           if (strlen($ipv4h_2)<2) { $ipv4h_2="0".$ipv4h_2; }
           if (strlen($ipv4h_3)<2) { $ipv4h_3="0".$ipv4h_3; }
           if (strlen($ipv4h_4)<2) { $ipv4h_4="0".$ipv4h_4; }
           $pre="";
           for ($i=0; $i<6; $i++) { $pre=$pre."0000:"; }
           $ipv6=$pre.$ipv4h_1.$ipv4h_2.":".$ipv4h_3.$ipv4h_4;
          
           echo "<p></p><p>IPv4 -address: \n";
           echo $clientIP."<br>IPv6 -address: ".$ipv6."</p>\n";
          

          Script can show little bad, because I doesn't know how to use arrays on PHP yet.

          LukeSW

            Well, don't know how much use this is to you, but I ran into the same problem this morning, and wrote a lil code to go through it. Since fopen() doesn't support proxies, and it doesn't look like there's any future plans to, I used fsockopen()

            function damnProxy($page, $proxyHost, $proxyPort) { 
                $data = fsockopen($proxyHost, $proxyPort);  
            if(!$data) { fclose($result);
            echo "Cannot access ".$page; }
            else {
            fputs($data,"GET $page HTTP/1.0\n\n");
            while(!feof($data)) { $cont=$cont.fread($data,4096); } } fclose($data); return substr($cont,strpos($cont,"\r\n\r\n")+4); } $slash = damnProxy("http://slashdot.org/slashdot.rdf", "proxy.yourcompany.com", 8080); if($slash) { echo $slash; } else { echo "damned proxy"; }

            Maybe with a couple changes, you can get it working for you too

              Ok, now it returns my ISP's proxy ip-address what is still wrong.

              LukeSW

                I upload my home page this ip.html -page:

                ip.html: 186.64.57.246

                And it returns "correct" (=same, 186.64.57.246) ip address.

                So I assume that code you publish, for some reason is not working right (=delivering wrong ip address).

                LukeSW

                edit: IP-addresses above are fictious.

                  Yes, it works! 😃

                  I was wrong code what I was uploaded to my homepage.

                  <?php
                  // echo(getenv ("REMOTE_ADDR"));
                  
                  $HCIP     = $_SERVER['HTTP_CLIENT_IP'];
                  $HXF      = $_SERVER['HTTP_X_FORWARDED'];
                  $HXFF     = $_SERVER['HTTP_X_FORWARDED_FOR'];
                  $RMAD     = $_SERVER['REMOTE_ADDR'];
                  
                  $clientIP = ($HCIP!="")? $HCIP :
                              ($HXF!="") ? $HXF  :
                              ($HXFF!="")? $HXFF :
                              ($RMAD!="")? $RMAD : "";
                  
                  echo $clientIP;
                  ?>

                  Code above did not work. When I changed it to below, it works.

                  <?php
                  $HCIP     = $_SERVER['HTTP_CLIENT_IP'];
                  echo $HCIP;
                  ?>

                  So, problem solved.

                  LukeSW

                    Yeah, the code I posted for for getting through proxies and opening a remote page.

                      Write a Reply...