In order not to wait a dozen seconds when things go wrong, you can set a couple more parameters, for example ( curl_setopt ):
CURLOPT_CONNECTTIMEOUT : The number of seconds to wait when trying to connect. Use 0 to wait forever.- setting it for a couple of seconds will be enough
CURLOPT_TIMEOUT : maximum number of seconds to execute cURL functions.CURLOPT_DNS_CACHE_TIMEOUT : The number of seconds to store DNS records in memory. By default, this option is set to 120 (2 minutes).- Maybe you can set this higher value
If users of your website tend to remain on it for more than one or two pages, it may be interesting to store this information in $_SESSION and receive it only once in a while.
For example, you can only get it if the value stored in the session was received more than 5 minutes ago. This would probably save a couple of calls :-)
Another way could be on the client side:
- select image with
<img> - in case of "online" use an image that loads OK
- connect the handler to "
load " even the image to replace it with text
- in case of "offline" use the image that is in 404
- connect the handler to "
error " even to the image to replace it with text
This is not very nice (itโs like โhackingโ, itโs not), but it should work; -)
Your image will look like this:
<div id="arround-1"> <img id="img-1" src="http://big.oscar.aol.com/imperialpalaces?on_url=http://www.ipalaces.org/support/widget/status_green.gif&off_url=http://this.is-a-404-error.com" onload="replace_img_status(1, 1);" onerror="replace_img_status(1, 0);" /> </div>
You see that if the user is connected, <img> ultimately leads to the existing image; therefore, " load " will even be started.
And in case the user is not connected, <img> will finally lead to an image that does not exist (it gives an error 404); therefore the event << 27> will be fired.
Now you need to take care of these two cases, for example:
<script type="text/javascript"> var replace_img_status = function (num, status) { var div = document.getElementById('arround-' + num); if (div) { if (status == 1) { div.innerHTML = 'Online'; } else { div.innerHTML = 'Offline'; } } }; </script>
If status is 1 , "Online" is displayed, and in another case ("error"), "Offline" is displayed: -)
But it even seems like it works, I don't really like this solution ^^