search
Homephp教程php手册利用Jquery+php显示当前在线用户

利用Jquery框架,Ajax调用当前在线用户

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. }

这个updateonline是div的ID。还有就是调用这个函数!
  1. $(document).ready(function(){
  2.    UpdateOnline(true);
  3.    window.setInterval("UpdateOnline(true)",60000);
  4. });

下面是服务端处理代码:
  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);

配合了session来存储IP地址以区分用户。

下面是XML文件
  1. 127.0.0.1



Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment