Log in

View Full Version : If this show image1, else show image2



Stinger
05-03-2010, 03:22 AM
Hey everyone,

I have a website that I'm putting together for an online gaming community, and I need to create or modify some kind of php code or script to basically ping the server and display an image being online if ping is successful, or offline if not.

I have a simple script here that might work (haven't been able to test it yet); will sometime as simple as that work? or is there anything else you can suggest I try.

<?PHP
$ip = "192.168.1.1";
$port = "27015";

if (! $sock = @fsockopen($ip, $port, $num, $error, 5)) {
print '<img src=img/button_serverstatus_offline.jpg>';
}
else {
print '<img src=img/button_serverstatus_online.jpg>';
fclose($sock);
}
?>

Thanks
Ryan

djr33
05-03-2010, 05:57 AM
I don't see why that wouldn't work. In fact you could remove the $sock = part and just use !@fsockopen().

But there are also other functions like file_get_contents(), though this may not be as versatile as fsockopen. (Well, it isn't-- but it might be in your case.)

The 'right' method is the one that works, and it's hard to tell without testing it. Does that work...? If not, try another method.

The basic logic is right: if (connection) { img1 } else { img2 }.


Note: you forgot the quotes in the HTML output. I think that'll give an error if you try to validate the code; it might even stop the images from displaying in some browsers.

Stinger
05-04-2010, 09:14 PM
thanks mate