|
-
Jan 26th, 2005, 09:02 AM
#1
Thread Starter
Lively Member
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?
-
Jan 26th, 2005, 10:28 AM
#2
Ex-Super Mod'rater
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.

-
Jan 26th, 2005, 12:18 PM
#3
Addicted Member
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
-
Jan 26th, 2005, 08:04 PM
#4
Re: check if a link is valid
 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; } } ?>
-
Jan 27th, 2005, 12:33 PM
#5
Addicted Member
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
-
Jan 27th, 2005, 12:40 PM
#6
Re: check if a link is valid
 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.
-
Jan 28th, 2005, 01:29 AM
#7
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)
-
Jan 29th, 2005, 08:14 PM
#8
Addicted Member
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
-
Jan 29th, 2005, 08:41 PM
#9
Re: check if a link is valid
 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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|