简体   繁体   中英

How to make auto refreshing page every minute in PHP

I want to make my site refreshing the page each minutes, Ex : 60sec/1min.I have index for put the code. Actually i make a home that recived a message from member, so the admin dashboard can take a look at inbox every minutes.Can some one give some documentation or example to do it using PHP??? pls not javascript nor ajax, thanks

<?php 
    header("Refresh: 60");
    session_start();
    include "conn.php";
    $koneksi=open_connection();

if (isset($_SESSION['id']))
{
    $id=$_SESSION['id'];
    $level = $_SESSION['level'];
    $username = $_SESSION['username'];
}else{
echo'<script>document.location.href="index.php?status=forbidden"</script>';
}

require_once('topbar.php');



require_once('sidebar.php');
                $page=(isset ($_GET['page']))? $_GET['page'] : 'main';
                    switch($page){
                        case 'data':include "halaman/data.php";
                            break;                   
                        case 'main':default: include 'beranda.php';
                    }
 require_once('footer.php');
?>

You can't. PHP ends it's work when page contents is sent to browser. And it can't influence on client side (browser). You can use JavaScript for this, use Ajax calls and setInterval() for this (as mentioned in comment above). Just create separate endpoint to which will you'll make request for new information every minute.

You can refresh a page using this: header("Refresh: 60"); But make sure you put this before any output, meaning that you cannot write even a space before your php code:

<?php 
    session_start();
    header("Refresh: 60");
    include "conn.php";
    $koneksi=open_connection();

if (isset($_SESSION['id']))
{
    $id=$_SESSION['id'];
    $level = $_SESSION['level'];
    $username = $_SESSION['username'];
}else{
echo'<script>document.location.href="index.php?status=forbidden"</script>';
}

require_once('topbar.php');

//**I want to put "auto refresh page" here**

require_once('sidebar.php');
                $page=(isset ($_GET['page']))? $_GET['page'] : 'main';
                    switch($page){
                        case 'data':include "halaman/data.php";
                            break;                   
                        case 'main':default: include 'beranda.php';
                    }
 require_once('footer.php');
?>

Although you have accepted one answer, I would like to tell you another way to do this. You said you need to display messages sent by the members of your website to the admin right? You can't use PHP alone. Atleast it is not very elegant to do it that way. You might want to take a look at HTML5 Server-Sent Events . When a new message appears in the database, you can send it automatically to the admin's web page without reloading it.

The code to make this work will involve JavaScript as well as PHP. Here is a rough example:

In your script tag of the webpage, open a connection to the server like this:

var messageEvent = new EventSource("some/relative/path/blah blah/message_updates.php");
//onopen, onmessage and onerror and different events that can occur
messageEvent.onmessage = function(event) {
    //Your logic to display the received data to frontend goes here. Example:
    //document.getElementById("messages").innerHTML += event.data;
    //if event.data is a JSON, then parse it and do stuff
};

Now the stream to the server stays open and any new messages will be sent to the client immediately. But before that, you need to put your PHP logic. So..

In your message_updates.php file:

 <?php
//Content-Type should compulsorily be text/event-stream
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

//write whatever logic you like to check the database for new messages here
$message=your_own_logic();
//$message should better be a JSON, like {"user":"dopedude","message":"hello"} etc. 
echo "data: $message";
//note that the messages should start with "data: ".
//In the client, this "data: " will be omitted automatically
?>

I will leave you with some links to proceed:

Mozilla docs

w3schools

PHP library for Server-Sent Events (optional, but recommended)

Simple step like this,

<!DOCTYPE html>
<html>
<head>
    <title>Autorefresh Browser using jquery</title>
    <script type="text/javascript" src="jquery.min.js"></script>
    <script type="text/javascript">
        $(function() {
            startRefresh();
        });
        function startRefresh() {
            setTimeout(startRefresh,100);
            $.get('text.html', function(data) {
                $('#viewHere').html(data);
            });
        }
    </script>

</head>
<body>
    <div id="viewHere"></div>
</body>
</html>

This video for complete tutorial https://youtu.be/Q907KyXcFHc

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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