Php refresh this page

Header() in PHP – Refresh (Redirect) to Location (URL) in X seconds

Recently, I was designing a simple user system for one of my websites. Obviously, it had login and logout feature. Now when I designed the login page, on a successful login, I wanted the user to be redirected to index page without displaying any kind of message. Whereas when I designed the logout page, I wanted the page to display “Logged out successfully” message for 5 seconds, then redirect to index page.

Now you can achieve this in HTML using the meta tag which I will be showing at the end of this tutorial, but if you wish to do this in PHP, it’s a little bit tricky. Following are the two cases and their solutions.

Case 1 : Redirect a page to a URL without waiting in PHP.

Say, after a successful login, you wish to redirect the user to index.php without waiting. Following is the code that can be used for the same :

Make sure that no text is sent to the browser before this part of the script is executed. Since header() is a function which is used to set “Headers” for a page when it is opened in a browser. In simpler words, when the browser starts receiving data from the server, it takes the header messages first and then the data that is supposed to be displayed. Now, if you send any data to be displayed, obviously you cannot send header messages any longer. So always make it a point to use PHP header() function before sending any kind of data to be displayed.

Case 2 : Redirect a page to an URL after waiting for X seconds in PHP.

Let us take the same example in the above scenario but let’s say that this time you wish to show a message saying logged in successfully, then wait for 5 seconds and redirect to index page. Here’s the code to do it :

Please note that I have used “echo” statement AFTER using “header()” function. I have briefly explained the logic behind this, in Case 1. If you use an echo statement or display any text before setting headers in a PHP page, the header function won’t work. So make sure you first set the headers and then use echo statement.

Bonus Tip 1: HTML code to redirect a webpage after X seconds.

Following is the code to redirect a webpage to an URL (for e.g. http://nimishprabhu.com) after, say, 5 seconds.

Just place the above code in the head section of the page i.e. after and before tag. This will refresh (redirect) a page to a specific URL after specified number of seconds.

Читайте также:  How to do java api testing

Bonus Tip 2: Javascript code to redirect a webpage after X seconds.

If you would like to use Javascript for some reason, then you can use setTimeout() function to achieve similar result, provided user has Javascript enabled.

You will replace “https://nimishprabhu.com” with your URL and 5000 (5000 milliseconds i.e 5 seconds) with desired number of milliseconds to wait before redirecting the page.

Hope this article was useful. If you have any doubts, feel free to drop a comment using the form below.

Источник

Refresh or Redirect a Page using PHP, With Examples

PHP Refresh Page

This article will show you how to refresh a web page in the browser using the PHP programming language.

It’s sometimes necessary to set a page to reload automatically, usually at some interval, to keep the page updated with changing information.

For example, you may have a scoreboard application that is displayed in a web browser on a projector and wish to have it periodically refresh to keep the displayed scores up to date with those stored.

Periodic refreshing is also used to redirect to a different page at a given interval, which could be used to emulate slideshow functionality for digital signage.

Refreshing the Page Using PHP

The PHP header function is used to set HTTP request headers – bits of information invisible to the end-user which tell the web browser about the data it is receiving.

The Refresh header tells the browser to refresh the page after a given number of seconds:

Above, the page is refreshed immediately, as 0 seconds is specified. To refresh after 3 seconds, you would use:

The Refresh header is not an official specification – most if not all browsers do heed it, but it is worth testing with your intended audience.

Redirecting to a Different Address Using PHP

The Refresh header also accepts an optional url if you wish to redirect to another page:

header("Refresh:0; url=another-page.php");

Above, when the PHP is executed during page load, it will immediately redirect to the another-page.php. Any kind of URL can be supplied – it doesn’t have to be a local file on your server.

It Could be Better to use JavaScript

Refreshing the page using PHP can be less than ideal. The user will have no interaction or ability to interrupt the refresh request, and you cannot perform any client-side operations as they may take longer than you expect – meaning your page is refreshed before tasks are complete and unexpected behavior may result.

It could be better to use JavaScript for this task – we’ve covered it in the below article.

Refreshing is a client-side operation. JavaScript is the client-side scripting language used by web browsers, so it can be better to do it this way rather than using server-side PHP to dictate what the client should do.

PHP might be preferable if you want search spiders such as Googlebot to crawl your redirects easily.

Источник

How to refresh a page using PHP.

In this tutorial, we are going to show you how to refresh a page using PHP.

Because PHP is a server-side language, we will need to send an HTTP header.

This header will tell the browser to refresh after a certain period of time.

How to refresh the current page with PHP.

Take a look at the following example:

//The number of seconds to wait before refreshing the current URL. $refreshAfter = 5; //Send a Refresh header to the browser. header('Refresh: ' . $refreshAfter);

In the PHP code above, we sent a Refresh header to the browser by using PHP’s header function.

Читайте также:  Java for list size

This meta refresh header tells the browser that it should refresh the current page after five seconds.

If you want to change the number of seconds that it takes, then you can simply modify the $refreshAfter variable.

If you inspect the HTTP response headers using Chrome Developer Tools or something similar, you will see the following:

HTTP Refresh Header

The PHP example above is equivalent to placing the following HTML meta tag in the head of your document:

You can also use this header to redirect to another URL after a certain period of time. For more information on how to do that, you can check out this guide.

Only refresh it once.

To refresh the page once, you will need to use a session variable like so:

//Start the session. session_start(); //If the session variable does not exist, //we will know that the page has not been refreshed yet. if(!isset($_SESSION['already_refreshed']))< //Number of seconds to refresh the page after. $refreshAfter = 5; //Send a Refresh header. header('Refresh: ' . $refreshAfter); //Set the session variable so that we don't //refresh again. $_SESSION['already_refreshed'] = true; >

In the example above, we used a session variable to prevent any further page refreshes.

If you run the code above, you will see that the header is not present on the second page load.

Источник

Refresh a Page in PHP

Refresh a Page in 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

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.

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

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.

#php 7.x  php echo(""); echo date('H:i:s Y-m-d'); ?> 

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.

#php 7.x  php echo date('H:i:s Y-m-d'); echo '  '; ?> 

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.

Источник

Refresh Page With PHP

Refresh Page With PHP

Web developers have multiple options for redirecting/reloading pages using either a server-side scripting language, like PHP, or client-side JavaScript. In this article, I will discuss the options you have for ‘refreshing’ a page using PHP, and why that word is a slight bit misleading.

Solid Tip: PHP is limited to using a meta tag to actually ‘refresh’ a page. ‘Redirecting’ a page is another story.

Why refresh is the wrong word

The term ‘refresh’ suggests that a page existed already. This makes sense on the client-side when using JavaScript, as a page that a visitor is looking at can be ‘refreshed’ at any moment with a simple function call. Refresh is an incorrect term when talking about PHP. Since PHP is a server-side language, it can only take you to a different URL before any part of the page has been sent to your browser.

Your options

It is usually more appropriate to use the term ‘redirect’ when talking about PHP. You can redirect a page, before any information has been sent out, by using PHP’s built-in header() function. This is useful when processing forms (logging in a user) or checking that certain conditions are met (sending unauthorized users back to login page). The following example shows you how to redirect a page.

 header("location: http://solidlystated.com"); ?>

The header function is also used when you want to take advantage of the limited refresh capability of meta tags. You have probably seen this when a page tells you, “You will be redirected in X seconds, or you can click here.” You can also simply echo the HTML for the meta tag to accomplish the same thing.

 // this refreshes current page after 5 seconds. header( "refresh:5;" ); // OR send to a new URL like this header( "refresh:5; url=http://solidlystated.com" ); // OR simply echo the HTML meta tag echo ''; ?>

Источник

Оцените статью