Use jquery+php to display current online users

Source: Internet
Author: User
Using the jquery framework, Ajax calls Current Online users

function Updateonline () {

    1. if (arguments[0]==true) {
    2. $.ajax ({
    3. URL: ' Ajax.php?do=updateonline ',
    4. Type: ' Get ',
    5. Success:function () {
    6. $ (' #updateonline '). html (Arguments[0])
    7. }
    8. }
    9. );
    10. }else{
    11. $.ajax ({url: ' ajax.php?do=updateonline ', type: ' Get '});
    12. }
    13. }

This updateonline is the ID of the div. There is also the call to this function!
    1. $ (document). Ready (function () {
    2. Updateonline (TRUE);
    3. Window.setinterval ("Updateonline (True)", 60000);
    4. });

Here is the service-side processing code:
  1. $Now =time ();
  2. $FileName = ' online.xml ';
  3. $XML =new DOMDocument;
  4. $XML->load ($FileName);
  5. $Items = $XML->getelementsbytagname ("item");
  6. $I = 0;
  7. $AddXML =true;
  8. foreach ($Items as $Item) {
  9. $IP = $Item->childnodes->item (0)->nodevalue;
  10. $Time = $Item->childnodes->item (1)->nodevalue;
  11. if ($IP ==getip ()) {
  12. $AddXML =false;
  13. }
  14. if ($IP ==@$_session[' IP ']) {
  15. $XML _online= $XML->getelementsbytagname ("Online")->item (0);
  16. $XML _online_item_select= $XML->getelementsbytagname ("Item")->item ($I);
  17. $XML _online_item= $XML->createelement ("Item");
  18. $NewIP = $XML->createelement ("IP", $IP);
  19. $XML _online_item->appendchild ($NewIP);
  20. $NewTime = $XML->createelement ("Time", $Now);
  21. $XML _online_item->appendchild ($NewTime);
  22. $XML _online->replacechild ($XML _online_item, $XML _online_item_select);
  23. $XML->save ($FileName);
  24. }
  25. if (Round ($Now-$Time)/60) >1) {
  26. $Item = $XML->documentelement->getelementsbytagname ("Item")->item ($I);
  27. $XML->documentelement->removechild ($Item);
  28. $XML->save ($FileName);
  29. }
  30. $I + +;
  31. }
  32. if ($AddXML) {
  33. $XML->formatoutput = true;
  34. $XML _online= $XML->getelementsbytagname ("Online")->item (0);
  35. $XML _online_item= $XML->createelement ("Item");
  36. $XML _online_item_ip= $XML->createelement ("IP", GetIP ());
  37. $XML _online_item->appendchild ($XML _online_item_ip);
  38. $XML _online_item_time= $XML->createelement ("Time", $Now);
  39. $XML _online_item->appendchild ($XML _online_item_time);
  40. $XML _online->appendchild ($XML _online_item);
  41. $XML->save ($FileName);
  42. $_session[' IP ']=getip ();
  43. }
  44. if ($Items->length==0) {
  45. echo ' 1 ';
  46. }else{
  47. Echo $Items->length;
  48. }
  49. Unset ($Now, $FileName, $XML, $Items, $I, $AddXML);

The session is used to store IP addresses to differentiate users.

The following is an XML file
    1. <?xml version= "1.0" encoding= "Utf-8"?>
    2. <online>
    3. <item><ip>127.0.0.1</ip><time>1232254720</time></item>
    4. </online>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.