我有一个非常简单的端口检查,我想发布在线/离线状态,我认为是一个动态图像。好吧,它不会给我和错误或者没有,如果它在线,但它不会离线或离线发布资源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);
?>
有人能给我这方面的有用信息吗?
这也是我的输出:
答案 0 :(得分:2)
这没有意义:
$status = (fsockopen($ip, $port));
if ($status) {
$online;
} else {
$offline;
}
这也是问题的原因,因为$status
永远不会是可打印的(条件中的行根本不会改变它的值)。它将是false
(在这种情况下,你将看不到任何你期望的“离线”)或资源(在这种情况下你会看到类似“资源ID#1”)。
用
替换所有上述代码$status = fsockopen($ip, $port) ? $online : $offline;
答案 1 :(得分:2)
更改
if ($status) {
$online;
} else {
$offline;
}
到
if ($status) {
$status = $online;
} else {
$status = $offline;
}
在if-block中只有$online
并不会自行执行任何操作。你必须做点什么;也就是说,将其分配给变量以便稍后输出。
答案 2 :(得分:2)
您正在将$status
打印到图像中,这是fsockopen()
调用的结果,而不是您在顶部指定的字符串。试试这个:
$status = (fsockopen($ip, $port));
if ($status) {
$status = $online;
} else {
$status = $offline;
}