To use fsockopen
, you need to remove schema ( http://
) and the slash at the end.
Example:
$host = 'pt.stackoverflow.com';
$conectado = fsockopen($host, 80);
if (is_resource($conectado)) {
print 'online';
}else{
print 'offline';
}
Alternatives
In addition to fsockpopen
, you can also use fopen
and curl
.
Example with fopen
:
$host = 'https://pt.stackoverflow.com/';
$conectado = fopen($host, 'r');
@$result = @stream_get_meta_data($conectado);
if (is_resource($conectado) && strpos($result['wrapper_data'][0], "200") !== false) {
print 'online';
}else{
print 'offline';
}
Example with curl
:
$host = 'http://pt.stackoverflow.com:80';
ob_start();
$ch = curl_init($host);
curl_exec($ch);
$info = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
ob_end_clean();
if( $info === 200 ) {
echo "Offline";
} else {
echo "Offline";
}