2013-04-27 97 views
0

我有一个PHP脚本,可以使用WoL在我的网络中的任何一台计算机上启动,检查它的连通性并输出状态。它正在通过尝试连接到一个端口来检查连接 - 比如说3389/RDP,并且当连接建立时,它输出“SUCCESS”,否则输出“FAILED”。为了让它检查连通性不止一次,它使用一个元标记来刷新页面。PHP/html:替代刷新

为了使它看起来不错,我有一个JS加载并显示PHP脚本的输出。但是当它检查一台机器的连通性时,它会刷新整个页面,因此只显示php脚本的输出,而没有其他内容。

如何让PHP脚本以其他方式重试连接?

这是PHP脚本:

<?php 
################################################## 
##  php WoL with port check - PRThomasUK. ## 
##    blog.pete-thomas.me.uk   ## 
################################################## 
// 
// ################## user variables... ################## 
// Friendly name for device, example "My Computer". 
$device  = "roadrunner"; 
// IP address of device, example "x.x.x.x". 
$deviceip = "10.0.0.20"; 
// TCP Port on device to check, example "3389" = RDP, "80" = WebServer. 
$deviceport = "3389"; 
// MAC address of NIC1 on device, example "00:00:00:00:00:00" or "00-00-00-00-00-00". 
$devicemac1 = "00-11-22-33-44-55"; 
// MAC address of NIC2 on device, example "00:00:00:00:00:00" or "00-00-00-00-00-00". - Optional set to "" if NIC2 not required. 
$devicemac2 = ""; 
// Number of times to attempt to connect to port on device after magic packet sent, example "10" 
$maxtries = "10"; 
// Broadcast address of network, example ""x.x.x.x". ("255.255.255.255" works in most cases.) 
$broadcast = "255.255.255.255"; 
// ICMP port number, default "7". 
$udport  = "7"; 
// Timeout value for re-tries (in seconds), default "10". 
$timeout = "2"; 
// ####################################################### 
// 
// ##### Read in variables from url ##### 
// 
// $frame - used to determine which content to display when executed. 
$frame  = "1"; 
// $tries - used to determine number of attempts at checking port beetween reloads, compared with maxtries. 
$tries  = $_GET[ "tries" ]; 
// $pageurl - obtain URL used to access file, used when creating frameset & setting reloads. 
$pageurl = pageurl(); 
// Process variables used in frame2, increments tries & sets status to Success(1) or Failed(2) 
if ($frame == 1) { 
       processurl(); 
} 
// ###### Functions ###### 
// 
// function pageurl() - Returns URL of page via PHP variables. 
function pageurl() 
{ 
       $pageurl = "HTTP"; 
       if ($_SERVER[ "HTTPS" ] == "on") { 
           $pageurl .= "S"; 
       } 
       $pageurl .= "://"; 
       if ($_SERVER[ "SERVER_PORT" ] != "80") { 
           $pageurl .= $_SERVER[ "SERVER_NAME" ] . ":" . $_SERVER[ "SERVER_PORT" ] . $_SERVER[ "REQUEST_URI" ]; 
       } else { 
           $pageurl .= $_SERVER[ "SERVER_NAME" ] . $_SERVER[ "REQUEST_URI" ]; 
       } 
       $urlarts = explode("?", $pageurl); 
       $pageurl = $urlarts[ "0" ]; 
       return $pageurl; 
} 
// function processurl() - Processes variables used in frame2, increments tries & sets status to Success(1) or Failed(2) 
function processurl() 
{ 
       global $status, $tries, $maxtries; 
       if ($status == 0 && $tries < $maxtries - 1) { 
           $tries = $tries + 1; 
       } else { 
           $status = 2; 
       } 
       if (portcheck() == 0) { 
           $status = 1; 
       } 
} 
// function wakeonlan() - Attempts to send WoL packet and returns outcome. 
function wakeonlan($device, $mac) 
{ 
       global $broadcast, $udport; 
       $mac   = preg_replace("/[^A-Za-z0-9]/", ":", $mac); 
       $broadcast_byte = explode(':', $mac); 
       $hw_addr  = ''; 
       for ($a = 0; $a < 6; $a++) 
           $hw_addr .= chr(hexdec($broadcast_byte[ $a ])); 
       $msg = chr(255) . chr(255) . chr(255) . chr(255) . chr(255) . chr(255); 
       for ($a = 1; $a <= 16; $a++) 
           $msg .= $hw_addr; 
       $s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
       if ($s == false) { 
           $content = "Error creating socket!\n"; 
       } else { 
           // setting a broadcast option to socket: 
           $opt_ret = socket_set_option($s, 1, 6, true); 
           if ($opt_ret < 0) { 
               $content = "setsockopt() failed, error: " . strerror($opt_ret) . "\n"; 
           } 
           if (socket_sendto($s, $msg, strlen($msg), 0, $broadcast, $udport)) { 
               $content = "WoL packet sent to mac address " . $mac . "...\n"; 
               socket_close($s); 
           } else { 
               $content = "Failed to send WoL packet!"; 
           } 
       } 
       return $content; 
} 
// function portcheck() - Attempts to connect to TCP port on the device via a Socket, returns $errno. 
function portcheck() 
{ 
       global $deviceip, $deviceport; 
       $file = fsockopen($deviceip, $deviceport, $errno, $errstr, 50); 
       if ($errno == 0) { 
           fclose($file); 
       } 
       return $errno; 
} 
// function htmlheader() - Returns HTML Header for TITLE and if Frame2 REFRESH set. 
function htmlheader() 
{ 
       global $device, $frame, $tries, $maxtries, $status, $pageurl, $timeout; 
       // global "custom" header settings 
       $content = "<TITLE>PHP WoL ($device) - by PRThomasUK </TITLE>\n"; 
       //generate refresh header for frame2. 
       if ($status == 0) { 
           $content .= "<META HTTP-EQUIV=\"refresh\" CONTENT=\"$timeout;url=$pageurl?&tries=$tries\">\n"; 
       } 
       return $content; 
} 
// function htmlheader() - Returns HTML content for mainpage, frame1 & frame2 based on value of $frame. 
function htmlcontent() 
{ 
       global $pageurl, $device, $deviceip, $deviceport, $devicemac1, $devicemac2, $frame, $tries, $maxtries, $status; 
       if ($frame == 1) { 
           if ($status == 0) { 
               $content = "<p>$tries/$maxtries retries failed.</p>\n"; 
           } elseif ($status == 1) { 
               $content .= "<p>SUCCESS</p>\n"; 
           } else { 
               $content .= "<p>FAILED</p>\n"; } 
       } else { 
           $content = "<FRAMESET rows=\"10,*\" frameborder=0 border=0>\n"; 
           $content .= "<FRAME SRC=\"$pageurl?frame=2\">\n"; 
           $content .= "<NOFRAMES>\n"; 
           if ($devicemac2) { 
               $content .= "<BR>\n"; 
               $content .= wakeonlan($device, $devicemac2); 
           } 
           $content .= "<BR>\n"; 
           $content .= "<BR>\n"; 
           $content .= "<FONT COLOR=\"red\">\n"; 
           $content .= "<H2>Your browser does not support frames...</H2>\n"; 
           $content .= "</FONT>\n"; 
           $content .= "<H3>Status of $device will not be monitored!</H3>\n"; 
           $content .= "</NOFRAMES>\n"; 
           $content .= "</FRAMESET>\n"; 
       } return $content; 
} 
?> 

<?php 
echo htmlheader(); 
echo htmlcontent(); 
?> 

,这是我的index.php:在一个函数(PHP页面的部分或ENCAPS的

<html> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> 
<title>WOL</title> 

<script type="text/javascript" src="jquery-1.9.1.min.js"></script> 
<script type="text/javascript"> 
function doSomething() { 
$('#mydiv').load('status1.php', function() {alert('Loaded!');}); 
} 
</script> 

<?php 
echo "10.0.0.20 &nbsp;&nbsp;&nbsp; roadrunner &nbsp;&nbsp;&nbsp;"; 
echo '<a href="#" onclick="doSomething()">power on</a>'; 
?> 
<div id="mydiv"></div> 

</html> 

回答

0

使用iframe

<html> 
<head> 
<meta content="text/html; charset=utf-8" http-equiv="Content-Type"> 
<title>WOL</title> 
</head> 
<body> 

<iframe src="status1.php"></iframe> 

</body> 
</html> 

顺便说一句,你应该学习一些关于HTML语法的知识。你的代码是废话。

+0

但这不会被链接或按钮触发,那只会在父页面加载时运行php脚本。我试着用href指定target =“framename”,但由于某种原因,这不起作用。 – dnn 2013-04-28 11:27:44

+0

使用javascript设置iframe的src。检查示例:http://jsfiddle.net/gCzqf/ – ghost 2013-04-28 11:52:01

+0

我切换到使用iframe。但不是使用JS来设置iframe的src,我只是在href /按钮中使用'target',因为它可以跨越iframe,而不仅仅是在活动页面/帧内。谢谢 :-) – dnn 2013-04-29 20:55:40

0

歇边部如的checkStatus( $ server =“all”);) 然后当你想加载页面时,默认情况下如果你没有提供一台计算机,你可以像往常一样加载页面,否则你可以说只刷新这部分。

+0

我不确定我是否理解你的答案是正确的。 PHP仍然需要以某种方式进行更新,即使我将连接检查放入自己的“部分”中?如果我为此单独制作一个JS,则输出将不会显示在网站上与第一个JS相同的位置(尽我所知) – dnn 2013-04-27 22:29:40