Are there any good solutions for checking the validity of IP address?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • xhe

    Are there any good solutions for checking the validity of IP address?

    Hi,
    I need to program to check the validity of IP address through PHP
    Initially I used this one:

    $url="http://www.ntc.gov.au/ViewPage.aspx?
    page=A024003045 00100020";

    $fp=fopen($url, "r");
    if(!$fp)
    {
    echo "Failed";
    }else
    {
    echo "success";
    }
    when I run it, it's output is "Failed",

    I then use

    $fp=fsockopen($ url,80); for second line, and also failed.

    This IP address is actually good.
    Butwhen I check "www.ntc.gov.au " it is success.

    So does that mean by using PHP, we can only check hostname? And if the
    IP address is longer and includes the appendix, we can not check it by
    PHP?

    Or do you have another good solutions?

    Thanks in advance.

    Frank

  • Rik

    #2
    Re: Are there any good solutions for checking the validity of IP address?

    On Thu, 22 Feb 2007 22:59:39 +0100, xhe <hexufeng@gmail .comwrote:
    Hi,
    I need to program to check the validity of IP address through PHP
    Initially I used this one:
    >
    $url="http://www.ntc.gov.au/ViewPage.aspx?
    page=A024003045 00100020";
    >
    $fp=fopen($url, "r");
    if(!$fp)
    {
    echo "Failed";
    }else
    {
    echo "success";
    }
    when I run it, it's output is "Failed",
    >
    I then use
    >
    $fp=fsockopen($ url,80); for second line, and also failed.
    >
    This IP address is actually good.
    Butwhen I check "www.ntc.gov.au " it is success.
    Where eactly are you checking the validity of an IP adress in you script?
    I can see no such effort, heck, I see no IP.
    --
    Rik Wasmus

    Comment

    • xhe

      #3
      Re: Are there any good solutions for checking the validity of IP address?

      Hi,
      The first line is the IP address that I am checking.
      It is
      $url="http://www.ntc.gov.au/ViewPage.aspx?p age=A0240030450 0100020";

      On Feb 22, 5:39 pm, Rik <luiheidsgoe... @hotmail.comwro te:
      On Thu, 22 Feb 2007 22:59:39 +0100, xhe <hexuf...@gmail .comwrote:
      Hi,
      I need to program to check the validity of IP address through PHP
      Initially I used this one:
      >
      $url="http://www.ntc.gov.au/ViewPage.aspx?
      page=A024003045 00100020";
      >
      $fp=fopen($url, "r");
      if(!$fp)
      {
      echo "Failed";
      }else
      {
      echo "success";
      }
      when I run it, it's output is "Failed",
      >
      I then use
      >
      $fp=fsockopen($ url,80); for second line, and also failed.
      >
      This IP address is actually good.
      Butwhen I check "www.ntc.gov.au " it is success.
      >
      Where eactly are you checking the validity of an IP adress in you script?
      I can see no such effort, heck, I see no IP.
      --
      Rik Wasmus- Hide quoted text -
      >
      - Show quoted text -

      Comment

      • Rik

        #4
        Re: Are there any good solutions for checking the validity of IP address?

        xhe <hexufeng@gmail .comwrote:
        The first line is the IP address that I am checking.
        It is
        $url="http://www.ntc.gov.au/ViewPage.aspx?p age=A0240030450 0100020";
        Call me insane, I see no IP address. Do you mean a DNS, or a URL?
        DNS:
        <http://www.php.net/pareseurl>
        <http://www.php.net/gethostbyname>

        URL checking should normally be possible with fopen(), if allow_url_fopen
        is enabled.
        --
        Rik Wasmus

        Comment

        • Jerry Stuckle

          #5
          Re: Are there any good solutions for checking the validity of IPaddress?

          xhe wrote:
          Hi,
          I need to program to check the validity of IP address through PHP
          Initially I used this one:
          >
          $url="http://www.ntc.gov.au/ViewPage.aspx?
          page=A024003045 00100020";
          >
          $fp=fopen($url, "r");
          if(!$fp)
          {
          echo "Failed";
          }else
          {
          echo "success";
          }
          when I run it, it's output is "Failed",
          >
          I then use
          >
          $fp=fsockopen($ url,80); for second line, and also failed.
          >
          This IP address is actually good.
          Butwhen I check "www.ntc.gov.au " it is success.
          >
          So does that mean by using PHP, we can only check hostname? And if the
          IP address is longer and includes the appendix, we can not check it by
          PHP?
          >
          Or do you have another good solutions?
          >
          Thanks in advance.
          >
          Frank
          >
          What do you mean by the "validity of the IP address"? For instance,
          your fsockopen only works if the target system has a web server running
          (same with fopen).


          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • xhe

            #6
            Re: Are there any good solutions for checking the validity of IP address?

            if we use this one
            if(fopen("http://www.ntc.gov.au" ,"r"))
            {
            echo "success";
            }else
            {
            echo "false";
            }
            result is false.

            with fsockopen, we can ONLY check "www.ntc.gov.au ", and we can not
            check if the whole url is reachable or not.
            It seemed that fsockopen is only valid to check host name, while not
            the whole ip string.

            Any good idea to check the who ip string instead of just host name?

            thanks.




            On Feb 22, 8:30 pm, Rik <luiheidsgoe... @hotmail.comwro te:
            xhe <hexuf...@gmail .comwrote:
            The first line is the IP address that I am checking.
            It is
            $url="http://www.ntc.gov.au/ViewPage.aspx?p age=A0240030450 0100020";
            >
            Call me insane, I see no IP address. Do you mean a DNS, or a URL?
            DNS:
            <http://www.php.net/pareseurl>
            <http://www.php.net/gethostbyname>
            >
            URL checking should normally be possible with fopen(), if allow_url_fopen
            is enabled.
            --
            Rik Wasmus

            Comment

            • Kimmo Laine

              #7
              Re: Are there any good solutions for checking the validity of IPaddress?

              xhe kirjoitti:
              Hi,
              I need to program to check the validity of IP address through PHP
              Initially I used this one:
              >
              $url="http://www.ntc.gov.au/ViewPage.aspx?
              page=A024003045 00100020";
              >
              $fp=fopen($url, "r");
              if(!$fp)
              {
              echo "Failed";
              }else
              {
              echo "success";
              }
              when I run it, it's output is "Failed",
              >
              I then use
              >
              $fp=fsockopen($ url,80); for second line, and also failed.
              >
              This IP address is actually good.
              Butwhen I check "www.ntc.gov.au " it is success.
              That's _not_ an IP address. IP is something like 12.34.56.78, four
              groups of digits between 0-255 separated with dots (...and then there's
              IPv6 address, which is six hexnumbers separated by a semicolon). What
              you have there are an url and a domain.

              check it out: http://en.wikipedia.org/wiki/IP_address

              --
              "En ole paha ihminen, mutta omenat ovat elinkeinoni." -Perttu Sirviö
              spam@outolempi. net | Gedoon-S @ IRCnet | rot13(xvzzb@bhg byrzcv.arg)

              Comment

              • Curtis

                #8
                Re: Are there any good solutions for checking the validity of IPaddress?

                xhe wrote:
                On Feb 22, 8:30 pm, Rik <luiheidsgoe... @hotmail.comwro te:
                >xhe <hexuf...@gmail .comwrote:
                >>The first line is the IP address that I am checking.
                >>It is
                >>$url="http://www.ntc.gov.au/ViewPage.aspx?p age=A0240030450 0100020";
                >Call me insane, I see no IP address. Do you mean a DNS, or a URL?
                >DNS:
                ><http://www.php.net/pareseurl>
                ><http://www.php.net/gethostbyname>
                >>
                >URL checking should normally be possible with fopen(), if
                allow_url_fopen
                >is enabled.
                >--
                >Rik Wasmus
                >
                if we use this one
                if(fopen("http://www.ntc.gov.au" ,"r"))
                {
                echo "success";
                }else
                {
                echo "false";
                }
                result is false.
                >
                with fsockopen, we can ONLY check "www.ntc.gov.au ", and we can not
                check if the whole url is reachable or not.
                It seemed that fsockopen is only valid to check host name, while not
                the whole ip string.
                >
                Any good idea to check the who ip string instead of just host name?
                >
                thanks.
                That's because you need to send the request using HTTP headers (if
                trying to communicate with a Web server). Also, you're checking if a
                resource exists on a server, you're not checking an IP address.
                www.ntc.gov.au is a domain.

                You should become best friends with Google for a while.

                --
                Curtis, http://dyersweb.com

                Comment

                • ctiggerf

                  #9
                  Re: Are there any good solutions for checking the validity of IP address?

                  On Feb 23, 12:49 am, Curtis <zer0d...@veriz on.netwrote:
                  xhe wrote:
                  <snip>
                  >
                  You should become best friends with Google for a while.
                  >
                  --
                  Curtis,http://dyersweb.com
                  /Agreed

                  But to answer your question (without reading the post that is) of how
                  to validate an ip address in php ...

                  <?php
                  $ip = '192.168.1';
                  if (($longip = ip2long($ip)) !== false) {
                  printf("%u<br>" , $longip);
                  if ($ip == long2ip($longip )) {
                  echo "IP: $ip appears to be ok";
                  } else {
                  echo "$ip does not seem to match " .
                  long2ip($longip );
                  }
                  } else {
                  echo "IP appears to be invalid";
                  }
                  ?>

                  For an explanation ...


                  Which just happened to be the first result on Google for "valid ip
                  address php".
                  --Chris F.

                  Comment

                  • shimmyshack

                    #10
                    Re: Are there any good solutions for checking the validity of IP address?

                    On 22 Feb, 21:59, "xhe" <hexuf...@gmail .comwrote:
                    Hi,
                    I need to program to check the validity of IP address through PHP
                    Initially I used this one:
                    >
                    $url="http://www.ntc.gov.au/ViewPage.aspx?
                    page=A024003045 00100020";
                    >
                    $fp=fopen($url, "r");
                    if(!$fp)
                    {
                    echo "Failed";
                    }else
                    {
                    echo "success";
                    }
                    when I run it, it's output is "Failed",
                    >
                    I then use
                    >
                    $fp=fsockopen($ url,80); for second line, and also failed.
                    >
                    This IP address is actually good.
                    Butwhen I check "www.ntc.gov.au " it is success.
                    >
                    So does that mean by using PHP, we can only check hostname? And if the
                    IP address is longer and includes the appendix, we can not check it by
                    PHP?
                    >
                    Or do you have another good solutions?
                    >
                    Thanks in advance.
                    >
                    Frank
                    Curtis answered this really but here is some code:

                    what you really mean is "how can I get php to request a web page to
                    check it?"
                    Well you need to get PHP to send all the right HTTP headers, the
                    traditional and best way is using the cURL library, examples on the
                    php website, it can handle settings of cookies and even SSL so it is
                    the best way but heres a quick script that will grab a url, if you use
                    a proxy put the IP and port at the top, other wise use the website
                    host and the port as I have don here:

                    #via a proxy
                    #$proxy_server = "127.0.0.1" ;
                    #$proxy_port = 8080;
                    #no proxy
                    $proxy_server = "www.bbc.co.uk" ;
                    $proxy_port = 80;

                    #homepage
                    $url_path = '/';
                    #something more adventurous
                    $url_path = '/go/toolbar/-/radio/d/';

                    $html = '';
                    $prox = fsockopen($prox y_server, $proxy_port, $errno, $errstr);
                    fputs($prox,"GE T " . $url_path . " HTTP/1.1\r\n");
                    fputs($prox,"HO ST: www.bbc.co.uk\r \n");
                    fputs($prox,"Us er-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-
                    GB; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1\r\n");
                    fputs($prox,"Ac cept: text/xml,application/xml,application/xhtml
                    +xml,text/html;q=0.9,text/plain;q=0.8,ima ge/png,*/*;q=0.5\r\n");
                    fputs($prox,"Ac cept-Language: en-GB,en,q=0.9,pt; q=0.8,fr;q=0.8, \r\n");
                    fputs($prox,"Ac cept-Encoding: gzip,deflate\r\ n");
                    fputs($prox,"Ac cept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 \r\n");
                    fputs($prox,"Ke ep-Alive: 300\r\n");
                    fputs($prox,"Pr oxy-Connection: keep-alive\r\n\r\n") ;
                    while (!feof ($prox))
                    {
                    $curline = fgets($prox, 4096);
                    if (substr($curlin e,-2, 2)!="\r\n")
                    {
                    $html .= $curline;
                    }
                    }
                    fclose($prox);
                    echo $html;

                    By default php doesnt send enough headers to trick the website into
                    thinking its not a bot. ALthough you can set the user-agent in the
                    php.ini file, its not enough to fool websites.

                    The reason why the above script doesnt look very good is that it makes
                    one request to the homepage.

                    But if you goto the bbc homepage first in a browser, then some of the
                    images might get cached and you will then see them if requested by the
                    html within the output as absolute src.

                    Hope that helps. Use cURL its better and faster, if you want to know
                    more download a proxy like paros or fiddlertool and install foxy proxy
                    for firefox2 and set it to pass your web requests through the proxy.

                    Comment

                    Working...