变量不发布到我认为的动态图像?

时间:2013-08-12 21:56:23

标签: php

我有一个非常简单的端口检查,我​​想发布在线/离线状态,我认为是一个动态图像。好吧,它不会给我和错误或者没有,如果它在线,但它不会离线或离线发布资源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);
?>

有人能给我这方面的有用信息吗?

这也是我的输出: enter image description here

3 个答案:

答案 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;
}