Need help in php

Discussion in 'PHP' started by luckmann, May 13, 2011.

  1. #1
    I wanna place an image on my website that will change depending on server status.
    So i found this php script that connects to ftp server.

    but here is what i really want and i don't want it to connect via ftp just standard image get
    
    Get onlineimage.gif from downloads.myserver.com if not availabe get offlineimage.gif from downloads.otherserver.com
    Code (markup):

    What i found
    <?
    
    $offimg="path_to_offline_image.gif";­
    $onimg="path_to_online_image.gif";­
    
    $ftp = fsockopen($HTTP_HOST, 21);
    
    if(!$ftp){ $status="<img src=\"$offimg\">"; } else { $status="<img src=\"$onimg\">"; }
    
    echo $status;
    
    ?>
    PHP:

    and can this be done in JS or html

    Thank you
     
    luckmann, May 13, 2011 IP
  2. anwaraa

    anwaraa Active Member

    Messages:
    167
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    90
    Digital Goods:
    2
    #2
    On whose server status? Yours or another server? If on another, you can use curl
     
    anwaraa, May 13, 2011 IP
  3. luckmann

    luckmann Peon

    Messages:
    272
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Well here is the thing:

    I have a forum that provides high speed download with no speed limits no waiting time, and no ads...

    so in order to provide such speed and so on i have installed my own server at home...
    the thing is i need to restart server from time to time and sometimes have it offline for upgrades

    but i need to have the members on my forum to know before they download

    so the webserver that is hosting my web forum will have the offline massage and my download server will have the online massage or image
     
    luckmann, May 13, 2011 IP
  4. JoelLarson

    JoelLarson Peon

    Messages:
    61
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    I'd recommend a server-side solution for this (in your case, PHP).

    <?php
    
    $hostname = 'google.com';
    $port = 21;
    
    $image = array();
    $image['online'] = 'online.png';
    $image['offline'] = 'offline.png';
    
    $ftp = @fsockopen($hostname, $port);
    
    $status = (!$ftp) ? 'online' : 'offline';
    
    // $status will be either online or offline.
    echo '<img src="'.$image[$status].' />';
    PHP:
    I hope this helps you for what you need. I haven't tested it though, so let me know if any issues rise.
     
    JoelLarson, May 13, 2011 IP
  5. hyperboards

    hyperboards Peon

    Messages:
    16
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    A way you could do it just using HTML/PHP would be:

    HTML:
    <img class="status" src="http://downloads.myserver.com/onlineimage.gif?<?=time()?>" alt="" onerror="this.src='http://downloads.anotherserver.com/offlineimage.gif';" />

    You need PHP to output the current time (onlineimage.gif?<?=time()?>) because you want to prevent browsers from caching the image. You want a fresh request for the image every time the page is loaded to get an up-to-date status on your server. If your server can't load the image, then the 'onerror' event is called, and we have set it to change the image's source to your offline image.

    If you don't want to use PHP and want a pure JS/HTML solution, you could do something like this:
    <script>
    var time = (new Date()).getTime();
    document.write('<img class="status" src="http://downloads.myserver.com/onlineimage.gif?'+time+'" alt="" onerror="this.src=\'http://downloads.anotherserver.com/offlineimage.gif\';" />');
    </script>

    I hope that helps you out. Let me know if something is off or if you hit any obstacles.
     
    hyperboards, May 14, 2011 IP
  6. luckmann

    luckmann Peon

    Messages:
    272
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    works like a charm you can see it here

    Thank you & to all who tried to help...
     
    luckmann, May 14, 2011 IP