PHP 8.5.0 Alpha 4 available for testing

Voting

: min(six, eight)?
(Example: nine)

The Note You're Voting On

Alexander Wegener
18 years ago
To check if a Url is Online (with http and https)
Using @fgets to hide Warning when using SSL
(Bug: "Warning: function.fgets SSL: fatal protocol error", http://bugs.php.net/bug.php?id=23220)

<?php

function isOnline($url) {
if (!
$url_info = parse_url($url)) {
return
false;
}

switch (
$url_info['scheme']) {
case
'https':
$scheme = 'ssl://';
$port = 443;
break;
case
'http':
default:
$scheme = '';
$port = 80;
}

$data = "";
$fid = @fsockopen($scheme . $url_info['host'], $port, $errno, $errstr, 30);
if (
$fid) {
fputs($fid, 'HEAD ' . (isset($url_info['path'])? $url_info['path']: '/') . (isset($url_info['query'])? '?' . $url_info['query']: '') . " HTTP/1.0\r\n" .
"Connection: close\r\n" .
'Host: ' . $url_info['host'] . "\r\n\r\n");
while (!
feof($fid)) {
$data .= @fgets($fid, 128);
}
fclose($fid);
return !empty(
$data);
} else {
return
false;
}
}

?>

<< Back to user notes page

To Top