是否可以在连续的ajax请求中为php变量分配一次值?

问题描述:

Well I have some simple text file, I am trying to read that file contents using php and update it to the user.I was setting up the ajax request for every 2 seconds but the problem is the file wont be updated for every 2 seconds may be 15 or 30 or 1 minute long.So i thought of doing this

check the last modified time of file and if it is different then only update it to the user but here where I am stucked at.

 Javascript functions

 function read()
 {
    read_text();
    t=setTimeout("read()",200);
 }

 function read_text()
 {
     var xmlhttp;
     if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
       xmlhttp=new XMLHttpRequest();
     }
     else
    {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
       {
          document.getElementById('read').innerHTML = xmlhttp.responseText;
       } 
    }
    xmlhttp.open("GET","demo.php",true);
    xmlhttp.send();
 }

My php file

<?php 
    $a = fileattime("filenam");
    $b = fileattime("filenam");
    if($b > $a )
    {
     echo "update contents";
     $a = $ b;
    }

?>

I thought to store the last modified time of file in $a variable and compare the value with $b variable but here $a will be overwritten with the new value for every ajax request. I want $a to be assigned a value only once, I mean for the first ajax request then later on compare it with $b value and if $b is greater than $a then update the contents to user then fill up $a with $b

but this is where i stucked at. Any help is greatly appreciated.Thanks

我有一些简单的文本文件,我正在尝试使用php读取该文件内容并将其更新给用户 我每隔2秒设置一次ajax请求,但问题是文件不会每2秒更新一次,可能是15或30或1分钟。所以我想这样做 p> 检查文件的最后修改时间,如果它不同,那么只将它更新给用户,但是我在这里更新。 p>

  Javascript functions 
 
 function  read()
 {
 read_text(); 
t = setTimeout(“read()”,200); 
} 
 
函数read_text()
 {
 var xmlhttp; 
 if(window)  .XMLHttpRequest)
 {//代码用于IE7 +,Firefox,Chrome,Opera,Safari 
 xmlhttp = new XMLHttpRequest(); 
} 
 else 
 {//代码用于IE6,IE5 
 xmlhttp = new ActiveXObject  (“Microsoft.XMLHTTP”); 
} 
 xmlhttp.onreadystatechange = function()
 {
 if if(xmlhttp.readyState == 4&amp;&amp; xmlhttp.status == 200)
 {  
 document.getElementById('read')。innerHTML = xmlhttp.responseText; 
} 
} 
 xmlhttp.open(“GET”,“demo.php”,true); 
 xmlhttp.send();  
} 
  code>  pre> 
 
 

我的php文件 strong> p>

 &lt;?php 
  $ a = fileattime(“filenam”); 
 $ b = fileattime(“filenam”); 
 if($ b&gt;  $ a)
 {
 echo“更新内容”; 
 $ a = $ b; 
} 
 
?&gt; 
  code>  pre> 
 
 

I 我想将文件的最后修改时间存储在 $ a code>变量中,并将该值与 $ b code>变量进行比较,但这里 $ a code>将被覆盖 每个ajax请求的新值。 我希望 $ a code>只分配一次值,我的意思是第一个ajax请求然后将它与 $ b code>值进行比较,如果 $ b 大于 $ a code>然后将内容更新为user,然后使用 $ b code> p> 填充 $ a code> n

但这是我坚持的地方。 非常感谢任何帮助。谢谢 p> div>

Try creating a session, and storing the information in a session variable.

http://us3.php.net/manual/en/reserved.variables.session.php

If you do that, you'll have a persistent value that you can use to test against to see the last modified / updated value.

Be sure to do session_start() at the beginning of the script.

You can have it look something like this.. JS:

// standard Ajax stuff
xmlhttp.open("GET", "demo.php?request=1", true);
xmlhttp.send();

PHP:

<?php
    session_start();
    if($_GET['request'] == 1) $_SESSION['modified_since'] = fileattime('filename');
    $a = $_SESSION['modified_since'];
    $b = fileattime("filename");
    if($b > $a) {
        echo "Update Contents";
        $_SESSION['modified_since'] = $b;
    }
?>

For every user should be assigned unique ID and the last time should be stored for example in database. Unique ID may be but in cookie after first request and then be used to find information in database.