Refresh a Page Using PHP

Refresh a page using PHP

You can do it with PHP:

header("Refresh:0");

It refreshes your current page, and if you need to redirect it to another page, use following:

header("Refresh:0; url=page2.php");

refresh a page once using php

header("Location: ".$_SERVER['REQUEST_URI']);

How to refresh the current page

You can't in PHP if you've already outputted something (since all headers must be sent before output begins).

If you haven't outputted:

header('Location:: current_page_url');
or
header("Refresh:0");

Better way is to do it through Javascript with the window object

$window.location.reload();

php refresh current page?

header('Location: '.$_SERVER['REQUEST_URI']);

How to make auto refreshing page every minute in PHP

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');
?>

PHP, refresh page on insert

Simple:

header('Refresh: 0'); // 0 = seconds

Even you can specify new location

header("Refresh:2; url=new_page.php");

But when working with header function there should not be anything echoed before calling it,
but if you have already echoed anything, then you can use html or javascript:

HTML

<meta http-equiv="refresh" content="0">
<!--here you can also specify new url location-->
<meta http-equiv="refresh" content="0; url=http://url.com/">

JS

window.location.reload();

Update: because you can't use header do this:

if ($stmt9) 
{
$message = "User updated Sussesfully!";
echo '<meta http-equiv="refresh" content="0">';
}
else
{
echo '<meta http-equiv="refresh" content="0">';
}

PHP refresh page after outputting some HTML (breaking along operation into chunks)

You might not consider this a pure PHP solution because there is an onload parameter specified for the <BODY> tag. Then again, purity is not all it's cracked up to be. But I throw this out as a technique you could use to give the user a running pseudo "progress bar" and your script would only get interrupted when you wanted it to and you would be able to pass back to the script any restart parameters. In this demo, the only "restart parameters" being passed back are successive integers that drive the progress bar, but you can get the idea:

<?php
if (!isset($_REQUEST['progress'])) {
// initially no parameters specified, so use starting value of 0:
$progress = 0;
}
else {
$progress = (int)$_REQUEST['progress'];
}
// increment progress:
$progress++;
// draw progress bar:
$progress_bar = str_repeat ('+' , $progress);

// simulate doing some work:
sleep(5);

// simulate being done or not:
$done = $progress == 5;
?>
<html>
<head>
<title>Test</title>
</head>
<?php if (!$done) { ?>
<body onload="document.f.submit();">
<?php } else { ?>
<body>
<?php } ?>
Progress: <?= $progress_bar ?><br>
<?php if (!$done) { ?>
<form name="f" method="post">
<input type="hidden" name="progress" value="<?= $progress ?>">
<!-- add any other hidden variables you need to resume where you left off -->
</form>
<?php } else { ?>
Done!
<?php } ?>
</body>
</html>


Related Topics



Leave a reply



Submit