如何判断主机是否在线

hashsha 2008-12-13 04:53:51
PHP中如何进行判断
...全文
174 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
海诗美妆 2008-12-13
  • 打赏
  • 举报
回复
关键要看lz要判断的是什么服务,
如果单纯看主机是否在线只能用ping,
如果是看http服务的话可以向服务器发送HEAD命令。

http://jp.php.net/manual/en/function.httpmessage-getheader.php
hashsha 2008-12-13
  • 打赏
  • 举报
回复
嗯,谢谢两位,我和gingzai777的想法一样,也想到了ping和端口判断,想看看还有没有其它方法。
Zijian_Zhang 2008-12-13
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 vinep 的回复:]
用下面的代码试试
PHP codefunctionget_content($url)
{for($m=0;$m<8&empty($content);$m++)//防止网络故障,一次读取不到,采取多次读取页面的方法{if(!strpos($url,'://'))return'Invalid URI';$content='';if(ini_get('allow_url_fopen'))
{$content=file_get_contents($url);
}elseif(function_exists('curl_init'))
{$handle=curl_init();
curl_setopt($handle,CURLOPT_URL,$url);
curl_setopt($handle,CURLOP…
[/Quote]你复制的吗??我的想法是:
判断能否连接到指定的主机,如用exec("ping /*主机名或IP*/");,还有ping有时检测不到,做一些端口判断,判断端口是否打开,打开肯定行呢,如果这个主机是服务器,那就更容易,搞个网页链接,能获得信息就是在线。
vinep 2008-12-13
  • 打赏
  • 举报
回复
用下面的代码试试

function get_content($url)
{
for($m=0;$m<8 & empty($content);$m++)//防止网络故障,一次读取不到,采取多次读取页面的方法
{
if(!strpos($url, '://')) return 'Invalid URI';
$content = '';
if(ini_get('allow_url_fopen'))
{
$content = file_get_contents($url);
}
elseif(function_exists('curl_init'))
{
$handle = curl_init();
curl_setopt($handle, CURLOPT_URL, $url);
curl_setopt($handle, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 0);
$content = curl_exec($handle);
curl_close($handle);
}
elseif(function_exists('fsockopen'))
{
$urlinfo = parse_url($url);
$host = $urlinfo['host'];
$str = explode($host, $url);
$uri = $str[1];
unset($urlinfo, $str);
$content = '';
$fp = fsockopen($host, 80, $errno, $errstr, 30);
if(!$fp)
{
$content = 'Can Not Open Socket...';
}
else
{
$out = "GET $uri HTTP/1.1\r\n";
$out.= "Host: $host \r\n";
$out.= "Accept: */*\r\n";
$out.= "User-Agent: $_SERVER[HTTP_USER_AGENT]\r\n";
$out.= "Connection: Close\r\n\r\n";
fputs($fp, $out);
while (!feof($fp))
{
$content .= fgets($fp, 4069);
}
fclose($fp);
}
}
}
if(empty($content)) $content = 'Can Not Open Url, Please Check You Server ... <br/>For More Information, Please Visit www.3wsite.cn';
return $content;
}

21,887

社区成员

发帖
与我相关
我的任务
社区描述
从PHP安装配置,PHP入门,PHP基础到PHP应用
社区管理员
  • 基础编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧