0

我有一个非常简单的端口检查,我​​希望将在线/离线状态发布到我认为是动态图像的位置。好吧,如果它在线,它不会给我和错误或什么都没有,但它不会离线发布或离线发布资源 id #1 这是我的代码:

<?php
$ip      = $_GET["ip"];
$port    = $_GET["port"];
$online  = "Online";
$offline = "Offline";

$status = (fsockopen($ip, $port));
if ($status) {
    $online;
} else {
    $offline;
}

// Create a blank image and add some text
$im         = imagecreatetruecolor(215, 86);
$text_color = imagecolorallocate($im, 233, 14, 91);

// sets background to Light Blue
$LightBlue = imagecolorallocate($im, 95, 172, 230);
imagefill($im, 0, 0, $LightBlue);

//Server Information
imagestring($im, 7, 5, 5, '3Nerds1Site.com', $text_color);
imagestring($im, 2, 40, 30, $ip, $text_color);
imagestring($im, 2, 40, 40, $port, $text_color);
imagestring($im, 2, 40, 70, $status, $text_color);

// Set the content type header - in this case image/jpeg
header('Content-Type: image/png');

// Output the image
imagepng($im);

// Free up memory
imagedestroy($im);
?>

谁能给我有关这方面的有用信息?

这也是我的输出: 在此处输入图像描述

4

3 回答 3

2

这是没有意义的:

$status = (fsockopen($ip, $port));
if ($status) {
    $online;
} else {
    $offline;
}

这也是问题的原因,因为它$status永远不会是可打印的(条件句中的行根本不会改变它的值)。它可能是false(在这种情况下,您将在您期望“离线”的地方看不到任何东西)或资源(在这种情况下,您将看到类似“Resource id #1”的内容)。

将以上所有代码替换为

$status = fsockopen($ip, $port) ? $online : $offline;
于 2013-08-12T21:59:06.237 回答
2

改变

if ($status) {
    $online;
} else {
    $offline;
}

if ($status) {
    $status = $online;
} else {
    $status = $offline;
}

仅仅拥有$online一个 if 块本身并不能做任何事情。你必须对它做点什么;即,将其分配给一个变量以稍后输出。

于 2013-08-12T21:59:10.980 回答
2

您正在打印$status到图像中,这是fsockopen()调用的结果,而不是您在顶部指定的字符串。尝试这个:

$status = (fsockopen($ip, $port));
if ($status) {
    $status = $online;
} else {
    $status = $offline;
}
于 2013-08-12T21:59:53.677 回答