The website is http://www.ipalaces.org/support/
The code I use for the status indicators is
<img src="http://big.oscar.aol.com/imperialpalaces?on_url=http://www.ipalaces.org/support/widget/status_green.gif&off_url=http://www.ipalaces.org/support/widget/status_offline.gif">
which is a neat thing that big.oscar.aol.com lets you do, it redirects it to whatever image you have set for the on_url if they are online, and same for off_url for offline. However, I want to use this in an if statement in PHP or javascript to display different things. Currently I am using this:
function getaim($screenname) {
$ch = curl_init();
$url = "http://big.oscar.aol.com/$screenname?on_url=true&off_url=false";
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
// added to fix php 5.1.6 issue:
curl_setopt($ch, CURLOPT_HEADER, 1);
$result = curl_exec($ch);
curl_close($ch);
if(eregi("true",$result)) {
return true;
} else {
return false;
}
}
If (getaim("ImperialPalaces")) { print "Online"; } else { print "Offline"; }
The problem with this code is that for some reason, at random times it can take up to 12 seconds for it to actually retrieve the results. Whereas the standard img trick is almost instant.
Is there a known issue with curl? Is there a faster way?
I've seen someone try to read the .src of the img tag and do an if statement like that, but I couldnt get it to work.
To avoid waiting for a dozen seconds when things are not doing OK, you can set a couple more options, like (see curl_setopt
) :
CURLOPT_CONNECTTIMEOUT
: The number of seconds to wait while trying to connect. Use 0 to wait indefinitely.
CURLOPT_TIMEOUT
: The maximum number of seconds to allow cURL functions to execute.
CURLOPT_DNS_CACHE_TIMEOUT
: The number of seconds to keep DNS entries in memory. This option is set to 120 (2 minutes) by default.
If users of your website generally stay on it for more than only one or two pages, it might be interesting to store that information in $_SESSION
, and only fetch it once in a while.
For instance, you could fetch it only if the value stored in session has been fetched more than 5 minutes ago. It would probably save a couple of calls :-)
One other way might be to do that on the client-side :
<img>
tagload
" even of the image, to replace it with some texterror
" even of the image, to replace it with some textThat's not very nice (it's kind of "hacky", in the wrong way), but it should work ;-)
Your image would be 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, the <img>
finally leads to an image that exists ; so, the "load
" even will be fired.
And, in the case the user is not connected, the <img>
will finally lead to an image that doesn't exist (it's giving a 404 error) ; so, the "error
" event will be fired.
You now have to take care of those two cases, with something like this :
<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
, we display "Online", and, in the other case ("error"), we display "Offline" :-)
But, even is it seems to be working, I don't really like that solution ^^
+1 to Pascal for that, but for anyone who wants this in PHP, and is still interested, I've made a little script which handles the header location (which returns offline/online or whatever you put on the end of the URL (For example, on_url=1, in the header would return Location: 1)
<?php
Define('Username', $_GET['sn']);
Define('URL', 'http://big.oscar.aol.com/' . Username . '?on_url=online&off_url=offline');
Define('Online', 'Location: online'); // Change online to whatever you have for on_url
Define('Offline', 'Location: offline'); // Change offline to whatever you have for off_url
$Response = Get_Headers(URL);
If(In_Array(Online, $Response)){
Echo UCWords(StrToLower(Username)) . ' is currently online.';
}
ElseIf(In_Array(Offline, $Response)){
Echo UCWords(StrToLower(Username)) . ' is currently offline, or has set their privacy settings.';
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With