how to validate a url

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Yang Li Ke

    how to validate a url

    Hi,

    Anyone can tell me how to validate
    if a web page is online or 404 ?

    Thanx

    Yang

    --


    -------------------------------------------------------------------------
    FIGHT BACK AGAINST SPAM!
    Download Spam Inspector, the Award Winning Anti-Spam Filter




  • Julien CROUZET aka c2c

    #2
    Re: how to validate a url

    Erwin Moller <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> writes:
    [color=blue]
    > Yang Li Ke wrote:
    >[color=green]
    > > Hi,
    > >
    > > Anyone can tell me how to validate
    > > if a web page is online or 404 ?
    > >[/color][/color]

    A portable an reliable method :

    404 :
    02/09 16:22 root@fusion ~# telnet localhost 80 Err 1 #16
    #I send :
    HEAD /does_not_exists .php HTTP/1.0

    #I receive
    HTTP/1.1 404 Not Found
    Date: Tue, 02 Sep 2003 14:24:04 GMT
    Server: Apache/1.3.26 (Unix) PHP/4.3.3 mod_ssl/2.8.10 OpenSSL/0.9.6g
    Connection: close
    Content-Type: text/html; charset=iso-8859-1

    online :
    #I send :
    HEAD / HTTP/1.0

    #Ireceive :
    HTTP/1.1 200 OK
    Date: Tue, 02 Sep 2003 14:25:03 GMT
    Server: Apache/1.3.26 (Unix) PHP/4.3.3 mod_ssl/2.8.10 OpenSSL/0.9.6g
    Content-Location: index.html.en
    Vary: negotiate,accep t-language,accept-charset
    TCN: choice
    Last-Modified: Wed, 29 Jan 2003 10:58:19 GMT
    ETag: "381a2-a71-3e37b3cb;3f546a d9"
    Accept-Ranges: bytes
    Content-Length: 2673
    Connection: close
    Content-Type: text/html
    Content-Language: en
    Expires: Tue, 02 Sep 2003 14:25:03 GMT

    So, You can easily use it with the help of fopen(), fputs(), etc....

    Good luck.

    --
    Julien CROUZET



    Comment

    • Erwin Moller

      #3
      Re: how to validate a url

      Yang Li Ke, still there??

      What Julien suggested is maybe not working.
      Here is a little script of my, and it does not output the headers.

      Julien, do you know why I do not receive any headers this way??

      ---------------------------------


      <?
      $theurl = @$_POST["theurl"];
      ?>

      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
      <html>
      <head>
      <title>urltes t</title>
      </head>
      <body>

      Check any URL:
      <form action="" method="POST" name="urlform">
      <input type="hidden" name="getdata" value="Y">
      <br>
      <input type=text size=40 name="theurl" value="<? echo $theurl; ?>">
      <br>
      <input type="submit" value="haal data">
      </form>

      <?
      if (isset($_POST["getdata"])) {
      // okay get the url
      // only works if track_errors = On is set in php.ini
      $php_errormsg = "";
      $handle = @fopen ($theurl, "r");
      if ($php_errormsg != "") {
      ?>
      Cannot open URL....
      <?
      } else
      {

      ?>
      <hr>
      <b>The content of <i>$theurl</i>:</b>
      <br>
      $php_errormsg= <? echo $php_errormsg; ?>
      <hr>
      <pre>
      <?
      while (!feof ($handle)) {
      $buffer = fgets($handle, 4096);
      echo htmlentities($b uffer);
      }
      fclose ($handle);
      ECHO "</pre>";
      }
      }
      ?>

      </body>
      </html>

      Comment

      • Julien CROUZET aka c2c

        #4
        Re: how to validate a url

        Erwin Moller <since_humans_r ead_this_I_am_s pammed_too_much @spamyourself.c om> writes:
        [color=blue]
        > Yang Li Ke, still there??
        >
        > What Julien suggested is maybe not working.
        > Here is a little script of my, and it does not output the headers.
        >
        > Julien, do you know why I do not receive any headers this way??
        >
        > ---------------------------------
        >
        >
        > <?
        > $theurl = @$_POST["theurl"];
        > ?>
        >
        > <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
        > <html>
        > <head>
        > <title>urltes t</title>
        > </head>
        > <body>
        >
        > Check any URL:
        > <form action="" method="POST" name="urlform">
        > <input type="hidden" name="getdata" value="Y">
        > <br>
        > <input type=text size=40 name="theurl" value="<? echo $theurl; ?>">
        > <br>
        > <input type="submit" value="haal data">
        > </form>
        >
        > <?
        > if (isset($_POST["getdata"])) {
        > // okay get the url
        > // only works if track_errors = On is set in php.ini
        > $php_errormsg = "";
        > $handle = @fopen ($theurl, "r");[/color]

        Here, you open the socket
        [color=blue]
        > if ($php_errormsg != "") {
        > ?>
        > Cannot open URL....
        > <?
        > } else
        > {
        >
        > ?>
        > <hr>
        > <b>The content of <i>$theurl</i>:</b>
        > <br>
        > $php_errormsg= <? echo $php_errormsg; ?>
        > <hr>
        > <pre>
        > <?
        > while (!feof ($handle)) {
        > $buffer = fgets($handle, 4096);[/color]

        here you read the socket till the server close the connection
        [color=blue]
        > echo htmlentities($b uffer);
        > }
        > fclose ($handle);
        > ECHO "</pre>";
        > }
        > }
        > ?>
        >
        > </body>
        > </html>[/color]

        You must send "HEAD /page.php HTTP/1.0\n\n" to have an answer.

        And be careful of "while (!feof(" cause sometimes apache
        doesn't close the connection after the answer, and wait for
        another query during {x} seconds.

        Close you socket once you received the status.

        Good luck again =)

        --
        Julien CROUZET

        Comment

        • Julien CROUZET aka c2c

          #5
          Re: how to validate a url

          "Yang Li Ke" <yanglike@sympa tico.ca> writes:
          [color=blue][color=green]
          > >$php_errorms g = "";
          > >$handle = @fopen ($theurl, "r");
          > >if ($php_errormsg != "") {[/color]
          >
          > I dont really understand how will the $php_errormsg get a value if u dont
          > put any $php_errormsg = something;
          >[/color]

          "The previous error message: $php_errormsg
          $php_errormsg is a variable containing the text of the last error
          message generated by PHP. This variable will only be available within
          the scope in which the error occurred, and only if the track_errors
          configuration option is turned on (it defaults to off)."

          It is a reserved variable.

          --
          Julien CROUZET

          Comment

          • Erwin Moller

            #6
            Re: how to validate a url

            Thanks for explaining Julien.
            :-)


            Comment

            Working...