How to Refresh a Page in PHP

Subodh Poudel Feb 02, 2024
PHP
  1. Use the header() Function to Periodically Refresh the Page in PHP
  2. Use the HTML meta Tag to Refresh the Page Periodically in PHP
  3. Use location.reload() JavaScript Function to Refresh the Page Periodically
How to Refresh a Page in PHP

We will introduce a method to refresh a page using a Refresh header in PHP. We can use this method to define a time interval for refreshing the page.

We will demonstrate another method to refresh a page using the HTML meta tag in PHP. This method is similar to the first one as we define the delay time for refreshing the page.

We will show you an example of how to refresh the page using the location.reload() JavaScript function. We use this function in a PHP script.

Use the header() Function to Periodically Refresh the Page in PHP

We can use the header() function to refresh the page in PHP. We write the Refresh header inside the header() function and specify the time interval for refreshing the page. For example, write a header() function and specify the time interval of 1 second as header("Refresh:1"). On the second line, use a date() function to display the current date and time. Use the characters H, i, s, Y, m, and d to represent hour, minute, second, year, month, and day, respectively. Use this format inside the date() function. Please check the PHP Manual to know about the header() function.

The example below refreshes the current time in one second. As a result, the current time will be displayed on the web page by the script. The output section of the code shows only an instance.

Example Code:

# php 7.*
<?php
header("Refresh:1");
echo date('H:i:s Y-m-d');
?>

Output:

14:45:19 2021-04-14

Use the HTML meta Tag to Refresh the Page Periodically in PHP

We can use the HTML meta tag to refresh the page periodically in PHP. We set the http-equiv attribute of the meta tag to refresh and specify the delay time in the content attribute. The http_equiv attribute sets the HTTP header for the value of the content attribute. For example, write a meta tag, specify the attribute http-equiv to refresh and the attribute content to 1 and close the tag. Display the current date and time using the date() function as in the above method. Check here to learn about the meta refresh.

The example below displays the real-time date and time on the web page. The page gets refreshed in one second, which enables this feature. The output section of the code displays only an instance of the time.

Example Code:

#php 7.x
<?php
echo("<meta http-equiv='refresh' content='1'>");
echo date('H:i:s Y-m-d');
?>

Output:

15:13:13 2021-04-14

Use location.reload() JavaScript Function to Refresh the Page Periodically

We can use the JavaScript function location.reload() to refresh a web page. We can use this function as well as in a PHP file. In context of PHP file, we echo the location.reload() function inside the script tag. The function takes boolean values as the parameter. The true value reloads the web page from the server, whereas the false value reloads the page with the browser’s data cached. The default value is false. Consult the MSDN Web Docs to learn more about the location.reload() function.

For example, in a PHP file, echo the date() function to display the current date and time. Then, write the function location.reload() inside the the script tag. Specify the type attribute as tex/javascript. Then, print the script tag using the echo statement.

Code Example:

#php 7.x
<?php
echo date('H:i:s Y-m-d');
echo '<script type="text/JavaScript"> location.reload(); </script>';
?>

Output:

15:53:25 2021-04-14
Subodh Poudel avatar Subodh Poudel avatar

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

LinkedIn