简体   繁体   English

PHP-一个php页面将在4秒的间隔内接收GET数据,而另一个页面将通过刷新自身来显示更新的数据

[英]PHP- One php page will receive GET data in 4 second interval and another page will display the updated data by refreshing itself

The question is one page will receive the GET data in 4 second interval and i have to display the parameter value in another page with updated value.It is just for display and no need to store the value. 问题是一页将在4秒的间隔内接收GET数据,我必须在另一页上显示具有更新值的参数值,这仅用于显示而无需存储值。

I have tried it with the following code: 我已经尝试使用以下代码:

datareceiver.php datareceiver.php

<?php
    require 'localStore.php';
    if(isset($_REQUEST['name'])){
        localStore::setData($_REQUEST['name']);
    }
?>


localStore.php localStore.php

<?php
    class localStore{
       private static $dataVal = "";
       public static function setData($data){
          self::$dataVal = $data;
       }
       public static function getData(){
          return self::$dataVal;
       }
    }
?>


display.php display.php

<?php
    require 'localStore.php';
    header("Refresh:2");
    echo localStore::getData();
?>


How i can show the data in 'display.php' page, because the above 'display.php' prints blank on the page. 我如何在“ display.php”页面中显示数据,因为上述“ display.php”页面上打印空白。 I tried also the global keyword concept and the PHP magic _construct(), _get() and _set() by searching in google and going through stackoverflow. 我也通过在Google中搜索并通过stackoverflow尝试了全局关键字概念和PHP魔术_construct(),_ get()和_set()。 but it was difficult to understand since i am not a PHP guy. 但由于我不是PHP专家,所以很难理解。 t was terrible for me to use these _get(),_set() and _construct(). 使用这些_get(),_ set()和_construct()对我来说太糟糕了。

PHP doesn't work like this. PHP不能像这样工作。 You have to store your data somewhere anyway. 无论如何,您都必须将数据存储在某个地方。 Script2 knows nothing about data recieved by script1. Script2对script1接收的数据一无所知。 When you construct your localStore object it doesn't have access to properties of another object constructed by another execution, cause your datareciever object was destructed immediately after script end. 当您构造localStore对象时,它无法访问由另一个执行构造的另一个对象的属性,这会导致您的datareciever对象在脚本结束后立即被销毁。 You can check it usind __destruct() method 您可以使用usind __destruct()方法进行检查

So, you need some storage to keep recieved by script1 data until script2 wants to display it. 因此,您需要一些存储空间以保留script1数据,直到script2想要显示它为止。 Most commonly used ways are: mysql DB, file and queue, say Rabbit. 最常用的方法是:mysql DB,文件和队列,例如Rabbit。

The simpliest solution is 最简单的解决方案是

   const FILE_NAME = 'tmp/anyname';
   public static function setData($data){
      return file_put_contents(self::FILE_NAME, serialize($data));
   }
   public static function getData(){
      $str = file_get_contents(self::FILE_NAME);
      return unserialize($str);
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM