Using the jquery framework, Ajax calls Current Online users
function Updateonline () {
- if (arguments[0]==true) {
- $.ajax ({
- URL: ' Ajax.php?do=updateonline ',
- Type: ' Get ',
- Success:function () {
- $ (' #updateonline '). html (Arguments[0])
- }
- }
- );
- }else{
- $.ajax ({url: ' ajax.php?do=updateonline ', type: ' Get '});
- }
- }
This updateonline is the ID of the div. There is also the call to this function!
- $ (document). Ready (function () {
- Updateonline (TRUE);
- Window.setinterval ("Updateonline (True)", 60000);
- });
Here is the service-side processing code:
- $Now =time ();
- $FileName = ' online.xml ';
- $XML =new DOMDocument;
- $XML->load ($FileName);
- $Items = $XML->getelementsbytagname ("item");
- $I = 0;
- $AddXML =true;
- foreach ($Items as $Item) {
- $IP = $Item->childnodes->item (0)->nodevalue;
- $Time = $Item->childnodes->item (1)->nodevalue;
- if ($IP ==getip ()) {
- $AddXML =false;
- }
- if ($IP ==@$_session[' IP ']) {
- $XML _online= $XML->getelementsbytagname ("Online")->item (0);
- $XML _online_item_select= $XML->getelementsbytagname ("Item")->item ($I);
- $XML _online_item= $XML->createelement ("Item");
- $NewIP = $XML->createelement ("IP", $IP);
- $XML _online_item->appendchild ($NewIP);
- $NewTime = $XML->createelement ("Time", $Now);
- $XML _online_item->appendchild ($NewTime);
- $XML _online->replacechild ($XML _online_item, $XML _online_item_select);
- $XML->save ($FileName);
- }
- if (Round ($Now-$Time)/60) >1) {
- $Item = $XML->documentelement->getelementsbytagname ("Item")->item ($I);
- $XML->documentelement->removechild ($Item);
- $XML->save ($FileName);
- }
- $I + +;
- }
- if ($AddXML) {
- $XML->formatoutput = true;
- $XML _online= $XML->getelementsbytagname ("Online")->item (0);
- $XML _online_item= $XML->createelement ("Item");
- $XML _online_item_ip= $XML->createelement ("IP", GetIP ());
- $XML _online_item->appendchild ($XML _online_item_ip);
- $XML _online_item_time= $XML->createelement ("Time", $Now);
- $XML _online_item->appendchild ($XML _online_item_time);
- $XML _online->appendchild ($XML _online_item);
- $XML->save ($FileName);
- $_session[' IP ']=getip ();
- }
- if ($Items->length==0) {
- echo ' 1 ';
- }else{
- Echo $Items->length;
- }
- Unset ($Now, $FileName, $XML, $Items, $I, $AddXML);
The session is used to store IP addresses to differentiate users.
The following is an XML file
- <?xml version= "1.0" encoding= "Utf-8"?>
- <online>
- <item><ip>127.0.0.1</ip><time>1232254720</time></item>
- </online>