Javascript or PHP to determine AIM status

Website http://www.ipalaces.org/support/

The code I use for status indicators,

<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 neat, which allows big.oscar.aol.com, it redirects it to any image that you set for on_url if they are online, and the same goes for off_url for offline. However, I want to use this in an if statement in PHP or javascript to display different things. I am currently 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, it may take up to 12 seconds to get actual results at random times. While the standard img trick is almost instantaneous.

Is there a known problem with curl? Is there a faster way?

I saw someone trying to read the .src of the img tag and make such an if statement, but I could not get it to work.

+6
javascript php curl status aim
source share
2 answers

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.
    • for this
  • 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&amp;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 ^^

+4
source share

+1 for Pascal for this, but for those who want it in PHP and still interested, I made a little script that handles the location of the header (which returns offline / online or whatever you put at the end of the url ( for example, on_url = 1, the location will be returned in the header: 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.'; } ?> 
+1
source share

All Articles