Display message before redirecting to another page

I have a simple question, but I do not know. I would like to first display the message before being redirected to another page, but my code simply redirects the page directly without displaying the message, or the display time is too short, I'm not sure. My code, as shown below, advises pls. thank you very much.

echo "Please Log In First"; header("location: login6.php"); 
+13
php
Aug 19 '13 at 1:19 on
source share
7 answers

You cannot do that. The PHP header must be sent before any content, so the correct order is:

 header("location: login6.php"); echo "Please Log In First"; 

But these codes will be redirected instantly and will not allow you to see the content. So I would do a JavaScript redirect:

 echo "Please Log In First"; echo "<script>setTimeout(\"location.href = 'http://www.forobd2.com';\",1500);</script>"; 
+28
Aug 19 '13 at 1:29 on
source share

You can use the header update. It will wait for the specified time before redirecting. Then you can display your message.

 header( "refresh:5; url=login.php" ); //wait for 5 seconds before redirecting 
+23
Aug 19 '13 at 1:24 on
source share

Update HTTP update to 5 seconds:

 header('Refresh:5; url=login6.php'); echo 'Please Log In First'; 
+6
Aug 19 '13 at 1:32
source share

Redirection is a redirection; -)

If you tell the browser using the Location: header to redirect to another page, the browser does just that: it redirects to another page. And the browser does it immediately - without any delay.

Therefore, your echo will not be displayed at all.

Set the headers first

In addition, you need to set headers before each other output operation (as indicated by Fred -ii- ):

 // First, echo headers header("location: login6.php"); // Then send any other HTML-output echo "Please Log In First"; 

Better not use auto-redirects

It is likely that you will not see the message, and then - after a certain time - automatically redirect to another page. People can get confused in this process. . Better do this:

Show the login page and representing the custom hinter or error message on this page.

Common decision

Prepare the component in the user session containing the information that will be displayed in the next instance of the script . This component can be a list of such messages:

 $_SERVER[ 'sys$flashMessagesForLater' ] = array( "Sorry, userID and password didn't match.", "Please login again." ); 

Each time a script request comes in , check if $_SERVER[ 'sys$flashMessagesForLater' ] non-empty array.

If so, output these values ​​to a clearly defined one located on the generated HTML page. A clearly defined location will always be in the same place, somewhere at the top of each page. You might want to add an error message box.

You might want to prepare the class as follows:

 class CFlashMessageManager { static public function addFlashMessageForLater( $message ) { $_SERVER[ 'sys$flashMessagesForLater' ][] = $message; } static public function flashMessagesForLaterAvailable() { return isset( $_SERVER[ 'sys$flashMessagesForLater' ] ) && is_array( $_SERVER[ 'sys$flashMessagesForLater' ] ) && ( 0 < count( $_SERVER[ 'sys$flashMessagesForLater' ] )) ; } static public function getFlashMessageForLaterAsHTML() { return implode( '<br />', $_SERVER[ 'sys$flashMessagesForLater' ] ); } } // CFlashMessageManager 
+6
Aug 19 '13 at 1:33 on
source share

You can redirect html: put this in the head

 <meta http-equiv="refresh" content="5; url=http://example.com/"> 

And the page will be redirected after 5 seconds (change 5 to the number of seconds you want)

+3
Aug 19 '13 at 1:24 on
source share

Working example in php.

 echo "<script>"; echo " alert('Import has successfully Done.'); window.location.href='".site_url('home')."'; </script>"; 
+2
Dec 05 '14 at 7:12
source share

try the sleep function

 <?php sleep(10);//seconds to wait.. echo "Please Log In First"; header("location: login6.php"); ?> 
-3
Aug 19 '13 at 1:24 on
source share



All Articles