douyun1950 2012-01-30 20:30
浏览 29
已采纳

是否可以在连续的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

  • 写回答

2条回答 默认 最新

  • drllqg2903 2012-01-30 20:33
    关注

    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;
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败