Refresh a Page Using PHP

In PHP, you can refresh a page using the header() function to send HTTP headers to the browser. This is useful for creating auto-refreshing pages or redirecting users after form submissions.

Using header() Function

The header() function sends HTTP headers to the browser. To refresh a page, you use the "Refresh" header with a specified delay time.

Syntax

header(string $header, bool $replace = true, int $http_response_code = 0): void

Parameters

  • $header The header string to send (e.g., "Refresh: 5")

  • $replace Whether to replace previous similar headers (default: true)

  • $http_response_code Optional HTTP response code

Example

Here's how to refresh a page after 5 seconds

<?php
// Delay in seconds before refreshing the page
$delay = 5;

// Redirect to the current page after the specified delay
header("Refresh: $delay");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Page Refresh Example</title>
</head>
<body>
    <h1>Page Refresh Example</h1>
    <p>This page will be refreshed automatically after <?php echo $delay; ?> seconds.</p>
</body>
</html>

Output

Page Refresh Example
This page will be refreshed automatically after 5 seconds.

How It Works

The code sets a $delay variable to 5 seconds. The header("Refresh: $delay") function sends a refresh instruction to the browser. The HTML content displays a message showing the refresh time. After 5 seconds, the browser automatically refreshes the page, creating a continuous refresh cycle.

Alternative Method

You can also redirect to a specific URL after refreshing

<?php
// Refresh and redirect to another page after 3 seconds
header("Refresh: 3; url=/dashboard.php");
?>

Conclusion

Using PHP's header() function with the "Refresh" header provides an effective way to automatically refresh pages. This method is ideal for real-time updates, periodic content changes, or creating simple auto-refreshing dashboards.

Updated on: 2026-03-15T10:38:23+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements