本文介绍了不断更新php页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个php页面,以显示一组五个图像中的一个图像.基于从文本文件读取的数据显示图像.另一个应用程序正在不断更新该文本文件中的数据.因此,只要文件更新,我的php页面就需要从该文件读取数据并基于该数据显示图像.我创建了一个无限循环来读取数据.但是,当我尝试从浏览器访问php页面时,由于无限循环,它没有加载.

I have created a php page to display a image from a set of five images. The image is displayed based on the data read from a text file.Another application is continuously updating data in that text file.So my php page need to read data from that file whenever the file is updated and display image based on that data. I created a infinite loop to read data. But when i tried to access the php page from a browser , it is not loading because of the infinite loop.

$myfile1 = fopen("readfile.txt", "r") or die("Unable to open file!");
$readJsonData = fread($myfile1,filesize("readfile.txt"));
fclose($myfile1);
$obj = json_decode($readJsonData);
$urllogo = 'tatalogo.png';

if(($obj->{"FrontLeft"}) == "TRUE")
{
    $url = 'images/FrontLeft.png';
}
else if(($obj->{"FrontRight"}) == "TRUE")
{
    $url = 'images/FrontRight.png';
}
else if(($obj->{"RearLeft"}) == "TRUE")
{
    $url = 'images/RearLeft.png';
}
else if(($obj->{"RearRight"}) == "TRUE")
{
    $url = 'images/RearRight.png';
}
else
{
    $url = 'images/Normal.png';
}

// infinite loop
while(1)
{
  //reading from the file and refreshing the page.
}

推荐答案

在PHP中设置标题,以刷新php页面

In PHP Set header like this to refresh the php page

header("Refresh: 5;url='pagename.php'");

在HTML Head标签中

In HTML Head tag

<html>
    <head>
    <meta http-equiv="refresh" content="5;URL='pagename.php'">
    </head>
</html>
<?php
Your php script here..
?>

使用JavaScript

Using Javascript

<script type="text/javascript>
window.setInterval(function(){
  reload_page();
}, 5000);
//Here 5000 in miliseconds so for 5 seconds use 5000
function reload_page()
{
  window.location = 'pagename.php';
}

这篇关于不断更新php页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 07:30