Php 如何实时更新文本文件

Php 如何实时更新文本文件,php,Php,我想做一个简单的聊天系统。我尝试了以下代码: <? $msgfilename=$_COOKIE['msgcookie']; session_start(); if (isset($_POST['txtmsg'])){ $type=$_POST['txtmsg']; echo $type; $mymsg=fopen($msgfilename,"a+")or die("unable t

我想做一个简单的聊天系统。我尝试了以下代码:

<?

     $msgfilename=$_COOKIE['msgcookie'];

    session_start();

    if (isset($_POST['txtmsg'])){

            $type=$_POST['txtmsg'];

            echo $type;

            $mymsg=fopen($msgfilename,"a+")or die("unable to open file");

            $msg=$type."<br>"."\n";

            fwrite($mymsg,$msg);

            fclose($mymsg);

    }

    //------------------------------------------------------------------------------------------

    $myfile = fopen($msgfilename, "r") or die("Unable to open file!");

    $file = file($msgfilename);

    for ($i = max(0, count($file)-25); $i < count($file); $i++) {

      echo $file[$i] . "\n";

    }

    fclose($myfile);

    echo '<form action="sendmsg.php" method="post" name="txtmsgf">';

    echo '<input type ="text" name="txtmsg" >';

    echo '<input type="submit" value="send">';

    ?>


当我输入消息并单击“发送”按钮时,我可以看到已发送的消息。但是如果不刷新网页,其他人就看不到我发送的消息。

PHP是一种服务器端编程语言。这意味着PHP所做的一切都不是在“客户端”计算机上完成的。PHP代码在服务器上运行,仅当客户端“请求”了解某个网页的外观时才执行某些操作,这也意味着计算机必须刷新该网页才能看到任何更改


有一个解决方案,但它不在于使用PHP,而是学习Ajax和/或asp.net或其他一些可以请求部分网页服务器请求的“客户端”语言。我不知道JavaScript是否有办法做到这一点,或者JS是否必须与上述建议结合使用。

@Niklesh如果你要编辑这篇文章,请修复整个文章。