Results 1 to 9 of 9

Thread: check if a link is valid

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2002
    Posts
    88

    check if a link is valid

    hi all,

    i need help for the following

    i upload a file to www.yousendit.com and it will generate a link to the file.the site allows 20 downloads of the file before the link expired

    how to write simple codes to check if the link still exist anot?

  2. #2
    Ex-Super Mod'rater Electroman's Avatar
    Join Date
    Sep 2000
    Location
    Newcastle, England
    Posts
    4,349

    Re: check if a link is valid

    I would expect sending a request for it will cause it to count that as a download so that wouldn't be possible.

    The only hope I can think of is it there is a web page that will tell you this status. If there is then your script should request that page then search for the part where it says how many downloads are left and use that .
    When your thread has been resolved please edit the original post in the thread ()
    and amend "-[RESOLVED]-" to the end of the title and change the icon to , Thank you.

    When posting Code use the [VBCode]Code Here[/VBCode] tags to be able to use the code highlighting.

  3. #3
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Re: check if a link is valid

    Just a thought...
    Maybe it won't be counted as a download if you use an HTTP HEAD request instead of an HTTP GET request.

    I'd assume that the PHP function get_headers is an HTTP HEAD request.
    EDIT: If it is not an HTTP HEAD request, then aeontech at gmail dot com has a real HTTP HEAD request at http://www.php.net/manual/en/function.get-headers.php if you page down.

    I'd expect you could also confirm this by using the telnet technique to send your HTTP HEAD requests and see if it counts toward a download.
    http://www.grumet.net/weblog/archive...d-example.html

    I never tried OPTIONS but this quote is interesting
    http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html section 9.2
    ...without implying a resource action or initiating a resource retrieval.
    Last edited by Phenix; Jan 26th, 2005 at 12:22 PM. Reason: aeontech at gmail dot com's modification
    Circa 1995
    Engineer - I think we should put our website address on our paper catalogs.
    Vice President - Don't get too excited about this internet thing.


    I am sorry, but the Oracle was mistaken. You cannot help us.
    -Matrix video game


    I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. ... and it probably never will support anything other than AT-harddisks, as that's all I have :-(.
    -Linus


    Question. Do you know that the character "?" means I'm asking a question? Question. Do you know that spoken inflection also provides the same cue? So please don't say, "Question" before you ask your question. Believe me I'll know.

    That said, I would have said this first if it had to precede what I'm telling you now. Having said that, what I'm telling you now is the same thing I just said about the annoying phrases "That said" and "Having said that".


    Are you threatening me, Master Jedi?
    -Chancellor Palpatine

  4. #4
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: check if a link is valid

    Quote Originally Posted by hongge
    hi all,

    i need help for the following

    i upload a file to www.yousendit.com and it will generate a link to the file.the site allows 20 downloads of the file before the link expired

    how to write simple codes to check if the link still exist anot?
    Hopefully this function should be what you want. Let me know if it doesn't work cos I wrote it a 1am in the morning

    PHP Code:
      <?php
      
          
    /* 
           * Returns the status of an HTTP URL
           *
           * Returns the status of the URL supplied. If the URL is valid
           * returning:
           *
           * true : the URL has been found on the server
           * false : the URL has not been found on the server and 
           *         a 404 response was returned by the server
           * -1 : host name not found
           * -2 : connection error (error notice produced)
           * -3 : the URL has been moved to a new location
           * -4 : other HTTP response (error notice produced)
           * -5 : unknown response (error notice produced)
           *
           * @author Adam Delves <adam at sccode dot com>
           * @param string url fully qualified url
           * @return integer status
           */
          
    function url_exists($url)
          {
              
    $url_info parse_url($url);
      
              if (! isset(
    $url_info['host'])) {
                  return 
    false;    
              }
              
              
    $port = (isset($url_info['post'])?$url_info['port']:80);
              
              if (! 
    $hwnd = @fsockopen($url_info['host'], $port$errno$errstr)) {
                  if (
    $errno == 97) { /* invalid hostname DNS error */
                      
    return -1;
                  } else {
                  
    trigger_error("Connection faliure: '$errno$errstr");
                      return -
    2;
                  }
              }
      
              
    $uri = @$url_info['path'] . '?' . @$url_info['query'];
              
              
    $http  "HEAD $uri HTTP/1.1\r\n";
              
    $http .= "Host: {$url_info['host']}\r\n";
              
    $http .= "Connection: close\r\n\r\n";
       
              @
    fwrite($hwnd$http);
      
              
    $response fgets($hwnd);
              
    $response_match "/^HTTP\/1\.1 ([0-9]+) (.*)$/";
      
              
    fclose($hwnd);
      
              if (
    preg_match($response_match$response$matches)) {
                  if (
    $matches[1] == 404) { /* not found response */
                      
    return false;
                  } else if (
    $matches[1] == 200) { /* found */
                      
    return true;
                 } else if (
    preg_match("/^30[0-9]$/"$matches[1])) { /* found somewhere else */
                      
    return -3;
                  } else {
                  
    trigger_error("HTTP Response: '{$mathes[1]}{$matches[2]}");
                      return -
    4;
                  }
              } else {
                  
    trigger_error("Bad response: $response");
                  return - 
    5;
              }       
          }
      
    ?>
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  5. #5
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Re: check if a link is valid

    If get_meta_tags and/or get_headers are really HTTP HEAD requests and the HEAD request does not count toward a download, then to check if it is a valid link, it could be as simple as
    Code:
    function url_exists($url){//PHP 3>= 3.0.4, PHP 4 , PHP 5
    	return is_array(@get_meta_tags($url));
    }
    
    //OR
    function url_exists($url){//PHP 5
    	return is_array(@get_headers($url));
    }
    
    //Or you can modify it to return an actual string "true" or "false".
    function url_exists($url){
    	if(is_array(@get_headers($url))){
    		return "true";
    	}else{
    		return "false";
    	}
    }
    Circa 1995
    Engineer - I think we should put our website address on our paper catalogs.
    Vice President - Don't get too excited about this internet thing.


    I am sorry, but the Oracle was mistaken. You cannot help us.
    -Matrix video game


    I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. ... and it probably never will support anything other than AT-harddisks, as that's all I have :-(.
    -Linus


    Question. Do you know that the character "?" means I'm asking a question? Question. Do you know that spoken inflection also provides the same cue? So please don't say, "Question" before you ask your question. Believe me I'll know.

    That said, I would have said this first if it had to precede what I'm telling you now. Having said that, what I'm telling you now is the same thing I just said about the annoying phrases "That said" and "Having said that".


    Are you threatening me, Master Jedi?
    -Chancellor Palpatine

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: check if a link is valid

    Quote Originally Posted by Phenix
    If get_meta_tags and/or get_headers are really HTTP HEAD requests and the HEAD request does not count toward a download, then to check if it is a valid link, it could be as simple as
    Code:
      function url_exists($url){//PHP 3>= 3.0.4, PHP 4 , PHP 5
      	return is_array(@get_meta_tags($url));
      }
      
      //OR
      function url_exists($url){//PHP 5
      	return is_array(@get_headers($url));
      }
      
      //Or you can modify it to return an actual string "true" or "false".
      function url_exists($url){
      	if(is_array(@get_headers($url))){
      		return "true";
      	}else{
      		return "false";
      	}
      }
    This wouldn't work because a 404 response which indicates the file has not been found is often accompanied by HTML which includes headers and meta tags. The meta tags are not the same as HTTP headers, they can be used to add additional headers to the pages but a page can validly contain no meta tags at all.

    Similarly using the get_headers() function will not work because all HTTP responses contain headers, even if the URL is not found.

    P.s: Look at the function I posted above, this is the only real way of checking for the existance of a link and even then in some cases the server may redirect the client instead.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: check if a link is valid

    You could use XMLHTTPREQUEST, so it can all be done client side.

    Code:
    var xmlhttp=false;
     try {
      xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (e) {
      try {
       xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
       xmlhttp = false;
      }
     }
    
    if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
      xmlhttp = new XMLHttpRequest();
    }
    xmlhttp.open("HEAD", "http://www.mendhak.com",true);
     xmlhttp.onreadystatechange=function() {
      if (xmlhttp.readyState==4) {
       if (xmlhttp.status==200) alert("URL Exists!")
        else if (xmlhttp.status==404) alert("URL doesn't exist!")
         else alert("Status is "+xmlhttp.status)
      }
     }
     xmlhttp.send(null)

  8. #8
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Re: check if a link is valid

    visualAd is right. I just did a telnet technique HEAD request to a file that doesn't exist, and got back an html file (not just the headers) that included head and meta tags. You will need to test for matching text in the response.

    But I think you still have to find out if the HEAD request to that file (or even an interrupted download) would still be counted as a download according to the server hosting the file.
    Circa 1995
    Engineer - I think we should put our website address on our paper catalogs.
    Vice President - Don't get too excited about this internet thing.


    I am sorry, but the Oracle was mistaken. You cannot help us.
    -Matrix video game


    I'm doing a (free) operating system (just a hobby, won't be big and professional like gnu) for 386(486) AT clones. ... and it probably never will support anything other than AT-harddisks, as that's all I have :-(.
    -Linus


    Question. Do you know that the character "?" means I'm asking a question? Question. Do you know that spoken inflection also provides the same cue? So please don't say, "Question" before you ask your question. Believe me I'll know.

    That said, I would have said this first if it had to precede what I'm telling you now. Having said that, what I'm telling you now is the same thing I just said about the annoying phrases "That said" and "Having said that".


    Are you threatening me, Master Jedi?
    -Chancellor Palpatine

  9. #9
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: check if a link is valid

    Quote Originally Posted by Phenix
    visualAd is right. I just did a telnet technique HEAD request to a file that doesn't exist, and got back an html file (not just the headers) that included head and meta tags. You will need to test for matching text in the response.

    But I think you still have to find out if the HEAD request to that file (or even an interrupted download) would still be counted as a download according to the server hosting the file.
    Am I the only one seeing the function I posted?

    The HEAD request does not count as a download as no data is transferred with this type of request.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width