在变量中包含整个页面(PHP)

时间:2018-04-20 04:27:37

标签: php events live

我正在尝试创建一个页面,我可以在不刷新页面的情况下强制更新客户端。所以我用Google搜索并用Google搜索并用Google搜索结果:http://www.developerdrive.com/2012/03/pushing-updates-to-the-web-page-with-html5-server-sent-events/ - 几乎成功

我设法使代码足够适应,以便我可以包含页面而不是使用示例rand(),因此,每当我修改该页面并将其上传到我的FTP时,页面都会更新。 但是,我无法弄清楚如何输出整个文件 - 只是该文件的第一行(maindiv.php)。我已经明白它与使用“file_get_contents”有关。如何在这里获得“maindiv.php”的完整输出?

我的网页代码是:

的index.php:

rn-nodeify --install --hack

send_sse.php:

<html>
<head>

<style type="text/css">
#serverData
{
    position:fixed;
    padding:0;
    margin:0;

    top:0;
    left:0;

    width: 100%;
    height: 100%;
    background:green;
}
</style>

</head>
<body>

<div id="serverData">Here is where the server sent data will appear



</body>

<script type="text/javascript">
//check for browser support
if(typeof(EventSource)!=="undefined") {
    //create an object, passing it the name and location of the server side script
    var eSource = new EventSource("send_sse.php");
    //detect message receipt
    eSource.onmessage = function(event) {
        //write the received data to the page
        document.getElementById("serverData").innerHTML = event.data;
    };
}
else {
    document.getElementById("serverData").innerHTML="Whoops! Your browser doesn't receive server-sent events.";
}
</script>
</html>

maindiv.php:

<?php
    header('Content-Type: text/event-stream');
    header('Cache-Control: no-cache');
    $maindiv = file_get_contents("maindiv.php"); //this does not read more than one line
    echo "data: $maindiv\n\n";
    ob_flush();
?>

0 个答案:

没有答案