Results 1 to 3 of 3

Thread: If this show image1, else show image2

  1. #1
    Join Date
    Feb 2009
    Location
    The land "Down Under"
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Smile If this show image1, else show image2

    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

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Feb 2009
    Location
    The land "Down Under"
    Posts
    10
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default

    thanks mate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •