search
HomeBackend DevelopmentPHP TutorialPHP实现聊天室的主动更新与被动更新_PHP

聊天的内容如何显示在屏幕上,一种是每隔一段时间刷新一次页面,读入全部聊天
内容,然后显示,这里采用的是js的document.write的方法实现不刷新的聊天页面
!

1 主页面的生成,规定了CSS类型,显示欢迎词
function write2(){
if(this.u.document==null)return;
this.u.document.writeln("");
this.u.document.writeln("ml; charset=gb2312>");
this.u.document.writeln("");

this.u.document.writeln("");
this.u.document.writeln("
//.................. 这里插入生成在线人数组的程序段


this.u.document.writeln("<script>"); <br />this.u.document.writeln("<p class=p9 align=left>"); <br />this.u.document.writeln("<p align=center>欢迎光临PlayBoy聊天室,本聊天室 <br />正在测试阶段,如有问题请与<a href=mailto:pccastle@sina.com>我们联系 <br /></script>

");
}

2 初始化进入信息,第一次进入聊天室

if($action == "enter")
{

/////////////////// 调用显示主屏幕的js程序 ////////////////////
print("parent.write2();n");

//发言内容,某某进入聊天室了
$message = "$name
到聊天室".$message." ".date("m月d日 H:i")."<script>parent.add('$name', <br />'$photo');parent.write1();</script>
";
}
//更新发言内容
while(file_exists($lockfile)){ $pppp++; }

//发言的锁定
fclose(fopen($lockfile,"w"));

//读入发言的总句数,也就是所有人一共发了多少言!我们可以保存每一个发言,但
是这样会占用大量的磁盘空间,我们采用了一种取模的方法,循环使用文件来减少
文件操作!
$talkmessage = file($filename);
$number = chop($talkmessage[0]);

//发言数增加一,然后保存
$talkhandle = fopen($filename,"w");
$number++;
fputs($talkhandle,$number);
fclose($talkhandle);

/去掉锁定
unlink($lockfile);

//对发言总数对10取模,作为文件名保存发言内容,也就是说第11句和第1句使用同
一个文件名,由于不可能同时有10句话没有更新,所以这是数在人不是非常多的情
况下很好!当然,考虑到人多的情况,可以设成100.
$filehandle = fopen("messageonline".($number%10).".php","w");
fputs($filehandle,$message);
fclose($filehandle);

//显示进入信息
print("parent.u.document.writeln("$message");n");

//调用主动刷新js程序,传递已经显示的发言数目
print("parent.flushwin($number)n");

//保存最后一次显示的发言
$last = $number;
}


3 处理发送表单的请求

//不处理空的发言和超过一定数目的发言
if( ($message != "")&&(strlen($message){

//检查发言者是否在线,防止意外
$onlineperson = file("useronline.dbf");
$personsign=0;
for($i=0;$i{
$person = split($split,$onlineperson[$i],99);
if($person[0] == $name)
{
$personsign = 1;
$person[3] = date("U");
break;
}
}

//在线时的处理程序
if($personsign == 1)
{

//添加发言时间的部分
$message = $message." ".date("m月d日 H:i")."
";


//锁定发言总数文件
while(file_exists($lockfile)){ $pppp++; }
fclose(fopen($lockfile,"w"));

//读入发言总数
$talkmessage = file($filename);
$number = chop($talkmessage[0]);

//总数加1,然后保存
$talkhandle = fopen($filename,"w");
$number++;
fputs($talkhandle,$number);
fclose($talkhandle);
unlink($lockfile);

//总数对10取模后以文件形式保存发言内容
$filehandle = fopen("messageonline".($number%10).".php","w");
fputs($filehandle,$message);
fclose($filehandle);
}
}

//////////////////////////////////////////////////////////////////
这样,表单的处理已经完成,下面的主动更新程序将会把新的发言内容显示在屏幕

//////////////////////////////////////////////////////////////////

4 主动更新的自动循环调用方法

可以使用&&pass= print($pass);
?>&&last="+winnumber;
flush.location=url
flushflag=false
}

//否则等待一个循环
flushtimerID=setTimeout("flushstart()",2000);
flushtimerRunning=true;
}

这种方法保证了在主程序不死的情况下,后台更新程序会一直运行下去!


5 主动更新程序



这样,每个发送的发言,经过被动更新程序处理保存到文件内,然后由一个循环的主
动更新程序完成显示任务!!!
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
How do you modify data stored in a PHP session?How do you modify data stored in a PHP session?Apr 27, 2025 am 12:23 AM

TomodifydatainaPHPsession,startthesessionwithsession_start(),thenuse$_SESSIONtoset,modify,orremovevariables.1)Startthesession.2)Setormodifysessionvariablesusing$_SESSION.3)Removevariableswithunset().4)Clearallvariableswithsession_unset().5)Destroythe

Give an example of storing an array in a PHP session.Give an example of storing an array in a PHP session.Apr 27, 2025 am 12:20 AM

Arrays can be stored in PHP sessions. 1. Start the session and use session_start(). 2. Create an array and store it in $_SESSION. 3. Retrieve the array through $_SESSION. 4. Optimize session data to improve performance.

How does garbage collection work for PHP sessions?How does garbage collection work for PHP sessions?Apr 27, 2025 am 12:19 AM

PHP session garbage collection is triggered through a probability mechanism to clean up expired session data. 1) Set the trigger probability and session life cycle in the configuration file; 2) You can use cron tasks to optimize high-load applications; 3) You need to balance the garbage collection frequency and performance to avoid data loss.

How can you trace session activity in PHP?How can you trace session activity in PHP?Apr 27, 2025 am 12:10 AM

Tracking user session activities in PHP is implemented through session management. 1) Use session_start() to start the session. 2) Store and access data through the $_SESSION array. 3) Call session_destroy() to end the session. Session tracking is used for user behavior analysis, security monitoring, and performance optimization.

How can you use a database to store PHP session data?How can you use a database to store PHP session data?Apr 27, 2025 am 12:02 AM

Using databases to store PHP session data can improve performance and scalability. 1) Configure MySQL to store session data: Set up the session processor in php.ini or PHP code. 2) Implement custom session processor: define open, close, read, write and other functions to interact with the database. 3) Optimization and best practices: Use indexing, caching, data compression and distributed storage to improve performance.

Explain the concept of a PHP session in simple terms.Explain the concept of a PHP session in simple terms.Apr 26, 2025 am 12:09 AM

PHPsessionstrackuserdataacrossmultiplepagerequestsusingauniqueIDstoredinacookie.Here'showtomanagethemeffectively:1)Startasessionwithsession_start()andstoredatain$_SESSION.2)RegeneratethesessionIDafterloginwithsession_regenerate_id(true)topreventsessi

How do you loop through all the values stored in a PHP session?How do you loop through all the values stored in a PHP session?Apr 26, 2025 am 12:06 AM

In PHP, iterating through session data can be achieved through the following steps: 1. Start the session using session_start(). 2. Iterate through foreach loop through all key-value pairs in the $_SESSION array. 3. When processing complex data structures, use is_array() or is_object() functions and use print_r() to output detailed information. 4. When optimizing traversal, paging can be used to avoid processing large amounts of data at one time. This will help you manage and use PHP session data more efficiently in your actual project.

Explain how to use sessions for user authentication.Explain how to use sessions for user authentication.Apr 26, 2025 am 12:04 AM

The session realizes user authentication through the server-side state management mechanism. 1) Session creation and generation of unique IDs, 2) IDs are passed through cookies, 3) Server stores and accesses session data through IDs, 4) User authentication and status management are realized, improving application security and user experience.

See all articles

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

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software