2014-12-04 81 views
2

我想要設置一個循環或可能是一個頁面刷新,反覆ping我的服務器,並告訴我毫秒。每隔1秒鐘無限次地關閉服務器?

這是我正在使用的代碼,但不知道如何使它保持清爽,並給我現場回覆。有人可以告訴我如何讓它生活,所以它每隔1秒不斷更新,甚至每2或3秒也是如此。只需要它活着。

<?php 

function pingDomain($domain){ 
    $starttime = microtime(true); 
    // supress error messages with @ 
    $file  = @fsockopen($domain, 80, $errno, $errstr, 10); 
    $stoptime = microtime(true); 
    $status = 0; 

    if (!$file){ 
     $status = -1; // Site is down 
    } 
    else{ 
     fclose($file); 
     $status = ($stoptime - $starttime) * 1000; 
     $status = floor($status); 
    } 
    return $status; 
} 
?> 

Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br> 

編輯與保羅的代碼仍然沒有運氣:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html;" /> 
    <meta http-equiv="refresh" content="1; url='yourPage.php'" /> 
</head> 
<body> 
<?php 

function pingDomain($domain){ 
    $starttime = microtime(true); 
    // supress error messages with @ 
    $file  = @fsockopen($domain, 80, $errno, $errstr, 10); 
    $stoptime = microtime(true); 
    $status = 0; 

    if (!$file){ 
     $status = -1; // Site is down 
    } 
    else{ 
     fclose($file); 
     $status = ($stoptime - $starttime) * 1000; 
     $status = floor($status); 
    } 
    return $status; 
} 
?> 

Server Latency: <?php echo pingDomain('192.168.1.20'); ?> ms<br> 
</body> 
</html> 
+0

這將是一個矯枉過正的+爲什麼重新發明輪子?有這麼多的開源服務器監控解決方案已經上市。 – 2014-12-04 07:44:32

+0

如何過度監控我的服務器,以便通過觀看此腳本,我可以在幾秒鐘內知道它的下載情況?我可以從手機或其他任何地方訪問腳本。我不需要一些高科技腳本就像這樣一個非常簡單的東西。如果你在某個方向上知道一點我。 – FrostByte 2014-12-04 07:54:29

+0

示例程序對我來說工作正常。你有什麼問題呢? – Volvox 2014-12-04 08:14:50

回答

0

有兩種方法我能想到的:

頁標題1.設置了刷新屬性

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <meta http-equiv="Content-Type" content="text/html;" /> 
    <meta http-equiv="refresh" content="1; url='yourPage.php'" /> 
</head> 
<body> 
<?php 
//your php code here 
?> 
</body> 
</html> 

2.使用crontab作業每秒執行一次該命令:

w3m http://yourhost/yourPage.php 

我認爲第一種解決方案更接近您的需求。

+0

我只是將其添加到php文件的頂部,現在它不會在頁面上顯示任何內容。 php腳本的名稱是latency.php,所以我在url =''部分添加了該部分,現在該頁面根本不顯示任何內容。 – FrostByte 2014-12-04 07:52:51

+0

hi @FrostByte,嘗試使用我的更新代碼 – 2014-12-04 07:56:58

+0

仍然沒有收到任何信息。我從頂部拿到了我的確切代碼,並將其放在身體標籤之間,仍然沒有任何東西。 – FrostByte 2014-12-04 08:00:42

-1

我會用這個javascript ajax請求。

+0

你會提出什麼建議?你有一個例子嗎? – FrostByte 2014-12-04 07:55:28