21

I have a simple question but yet i don't know. I would like to display message first before redirect to other page, but my code just directly redirect the page without display the message, or the display time is too short am not sure.. my code as below.. pls advise. thank much.

echo "Please Log In First";
header("location: login6.php");
user2649074
  • 229
  • 1
  • 3
  • 8
  • 3
    *"...but my code just directly redirect the page without display the message,"* <= **Impossible**. You can't use echo before header, you will get an error message `Headers already sent` – Funk Forty Niner Aug 19 '13 at 01:35
  • hi sir. i didn't get the error msg, but just redirect directly.. thx.. – user2649074 Aug 19 '13 at 02:22

7 Answers7

37

You can't do it that way. PHP header must sent out before any content, so the correct order is:

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

But these codes will redirect instantly and wouldn't let you see the content. So I would do the redirection by JavaScript:

echo "Please Log In First";
echo "<script>setTimeout(\"location.href = 'http://www.example.com';\",1500);</script>";
Sunry
  • 602
  • 4
  • 11
27

You can use header refresh. It will wait for specified time before redirecting. You can display your message then.

header( "refresh:5; url=login.php" ); //wait for 5 seconds before redirecting
Konsole
  • 3,447
  • 3
  • 29
  • 39
  • This practice is not recommended. See http://stackoverflow.com/questions/283752/refresh-http-header – Salman A May 02 '14 at 14:06
  • http://stackoverflow.com/questions/283752/refresh-http-header says that as well as not being standard the refresh header also causes performance issues in Internet Explorer. – Edward Apr 06 '16 at 19:01
9

HTTP refresh redirect to wait 5 seconds:

header('Refresh:5; url=login6.php');
echo 'Please Log In First';
federico-t
  • 12,014
  • 19
  • 67
  • 111
  • @Fred-ii- You're right, just realize that. The message should be echoed _after_ the header. – federico-t Aug 19 '13 at 01:42
  • sir.. i don't quite understand the logic.. why redirect the page first before display the message ?? I am applying this. and it works.. thanks so much.. – user2649074 Aug 19 '13 at 02:19
9

A redirect is a redirect ;-)

If you tell a browser by using a Location: header to redirect to another page, the browser does just this: It redirects to the other page. And the browser does this immediately - without any delay.

Therefore, your echo won't display at all.

Set headers first

Furthermore, you need to set headers before each other output operation (as pointed out 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

Quite likely, you won't show a message and then - after a certain amount of time - automatically redirect to another page. People might get confused by this process. Better do this:

Show the login-page and present a user-hinter or error-message on this page.

General solution

Prepare a component in the user's session, that contains information to be displayed at the next script instance. This component might be a list of messages like this:

$_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' ] is a non-empty array.

If this is the case, emit these values to a well-defined located on the generated HTML-page. A well-defined location would always be at the same location, somewhere at the top of each page. You might probably wish to add a read box around error messages.

You might want to prepare a class like this:

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
Community
  • 1
  • 1
SteAp
  • 11,853
  • 10
  • 53
  • 88
  • *"Therefore, your echo won't display at all."* Not only that, but [**this**](http://stackoverflow.com/questions/18305258/display-message-before-redirect-to-other-page#comment26859956_18305258) – Funk Forty Niner Aug 19 '13 at 01:38
  • thanks for your time explaining this to me.. this is too deep for me.. am new and noob.. :( – user2649074 Aug 19 '13 at 02:15
  • I think this would be the better answer, as the current accepted answer is not working if the user disables JavaScript... – Gigala Oct 26 '13 at 19:09
3

You can do a html redirection : put this in you head

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

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

Alabate
  • 74
  • 1
  • 3
  • 12
3

Working example in php.

echo "<script>";
echo " alert('Import has successfully Done.');      
        window.location.href='".site_url('home')."';
</script>";
Arslan Tabassum
  • 900
  • 1
  • 10
  • 25
-4

try sleep function

<?php
sleep(10);//seconds to wait..
echo "Please Log In First";
header("location: login6.php");
?>
chinna_82
  • 6,353
  • 17
  • 79
  • 134
  • Sleep just delays the PHP script execution at the server-side. Therefore, you earned down-votes. – SteAp Aug 19 '13 at 01:47