Jump to content

url_exists() function -- How can I get it to recognize spaces?


hatrickpatrick

Recommended Posts

I found this function in a php.net discussion, it checks to see whether a url exists or not. I REALLY need to get this working for my site, and it just about does - problem is, if the URL has a space in it, it always returns false, even if the URL does actually exist. Can anyone figure out how I can make it "spaces compliant"? I've tried changing the space in my $url variable to a + and to a %20, niether worked...

 

function url_exists($url) {
$a_url = parse_url($url);
if (!isset($a_url['port'])) $a_url['port'] = 80;
$errno = 0;
$errstr = '';
$timeout = 30;
if(isset($a_url['host']) && $a_url['host']!=gethostbyname($a_url['host'])){
$fid = fsockopen($a_url['host'], $a_url['port'], $errno, $errstr, $timeout);
if (!$fid) return false;
$page = isset($a_url['path'])?$a_url['path']:'';
$page .= isset($a_url['query'])?'?'.$a_url['query']:'';
fputs($fid, 'HEAD '.$page.' HTTP/1.0'."\r\n".'Host: '.$a_url['host']."\r\n\r\n");
$head = fread($fid, 4096);
fclose($fid);
return preg_match('#^HTTP/.*\s+[200|302]+\s#i', $head);
} else {
return false;
}
}

 

Help would be very much appreciated

Could you post the full URL you are trying to use.

 

That function seems a little akward. You could do exactly what that is doing a lot simpler with something like:

$url = get_headers($url);
if (ereg("200", $url[0]))
    echo "it is a url!";

 

It doesn't check if the url is an ip, like that function does, but it certainly works to find if a url exists.

They are represented as %20. PHP will read that. When a form submits it changes most invalid characters to their % representation. If you are manually making the link, just put a %20 where the space should be. The code I posted should work fine with that. You can use url_decode to change them back again.

http://us.php.net/manual/en/function.urldecode.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.