简体   繁体   English

php页面每60秒仅刷新div部分

[英]php page refresh only div section every 60 seconds

I have a php webpage and this prints html commands and do something else. 我有一个php网页,它可以打印html命令并执行其他操作。 I have a div section: 我有一个div部分:

<div id="monitor" class="monitoring"><?php include "php/monitoring.php"?></div>

This script in the index.php script should be called every 60 seconds. index.php脚本中的此脚本应每60秒调用一次。 So I found AJAX and JavaScript useful to do this. 因此,我发现AJAX和JavaScript对此很有用。 Therefore I use this script: 因此,我使用以下脚本:

<script type="text/javascript">
window.setInterval(function () {executemonitor()}, 60000);

function executemonitor() {
    $("#monitor").load("php/monitoring.php");
}

The scripts works fine, but the problem is, that second run of the monitoring.php script fails. 脚本工作正常,但是问题是,monitoring.php脚本的第二次运行失败。 The script is not able to use the $_SESSION variables. 该脚本无法使用$ _SESSION变量。 I get the error: Notice: Undefined variable: _SESSION in C:\\xampp\\htdocs\\mi-hand\\php\\monitoring.php on line 168. 我收到错误消息: 注意:未定义的变量:168行上C:\\ xampp \\ htdocs \\ mi-hand \\ php \\ monitoring.php中的_SESSION。

If I use the html refresh command, it works. 如果我使用html refresh命令,它会起作用。

<meta http-equiv="refresh" content="n" />

But then the whole page is refreshed. 但随后整个页面将刷新。 I want to avoid this. 我想避免这种情况。 I want only refreshing the div section, by executing the php script. 我只想通过执行php脚本刷新div部分。 How can I do this? 我怎样才能做到这一点? The monitoring.php file is a separate HTML page. monitoring.php文件是一个单独的HTML页面。

the _SESSION variable is available in php only after session_start() has been called. _SESSION变量仅在session_start()被调用后才在php中可用。

your client calls 2 different scripts : 您的客户调用了两个不同的脚本:

  • index.php which renders the div and includes monitoring.php index.php呈现div并包含monitoring.php
  • monitoring.php which renders the content of the div Monitoring.php呈现div的内容

you have a session_start() in index.php but not in monitoring.php so this cannot work when the client calls directly monitoring.php. 您在index.php中有一个session_start(),但在Monitoring.php中却没有,因此当客户端直接调用Monitoring.php时,这将无法正常工作。

You could create a session.php file which does the session initialization (like calling session_start()) and then in both index.php & monitoring.php use 您可以创建一个session.php文件来进行会话初始化(例如调用session_start()),然后在index.php和monitoring.php中使用

include_once('session.php');

if you have php > 5.4 you could also add in monitoring.php the following code 如果您的php> 5.4,也可以在monitoring.php中添加以下代码

if (session_status() == PHP_SESSION_NONE) {
  session_start();
}

Source: http://php.net/manual/en/function.session-status.php 资料来源: http : //php.net/manual/en/function.session-status.php

如果直接访问"php/monitoring.php" ,则需要在该脚本的开头使用session_start() ,而不仅是在index.php

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

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