Click to See Complete Forum and Search --> : check if a link is valid
hongge
Jan 26th, 2005, 08:02 AM
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?
Electroman
Jan 26th, 2005, 09:28 AM
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 :).
Phenix
Jan 26th, 2005, 11:18 AM
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/archives/http-head-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.
visualAd
Jan 26th, 2005, 07:04 PM
hi all,
i need help for the following
i upload a file to www.yousendit.com (http://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
/*
* 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;
}
}
?>
Phenix
Jan 27th, 2005, 11:33 AM
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
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";
}
}
visualAd
Jan 27th, 2005, 11:40 AM
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
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.
mendhak
Jan 28th, 2005, 12:29 AM
You could use XMLHTTPREQUEST, so it can all be done client side.
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)
Phenix
Jan 29th, 2005, 07:14 PM
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.
visualAd
Jan 29th, 2005, 07:41 PM
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.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.