1

I'm not asking for sleep(). I'm aware of that function.

if(mysql_num_rows($result) == 1){

//DELAY TO OCCUR HERE
echo "You are a validated user.";
    header('Location: usermainpage.php');
    }

If I use the sleep function, it simply delays loading the page. I want the echo to appear and then a redirect to occur.

stealthmode
  • 423
  • 1
  • 6
  • 13
  • 3
    That's not how redirects work. If you want the page to render first then the redirect should happen client-side, possibly with JavaScript. – David Apr 16 '13 at 17:46
  • 2
    you could just echo ''; – Kai Qing Apr 16 '13 at 17:47
  • php has to load the page, if you delay in php you delay loading the page. If you want page to load but delay in printing info to screen and re-directing then consider using php to give the info then JS to print and re-direct – Kevin Lynch Apr 16 '13 at 17:48
  • Please don't do this. Just issue the redirect and move on. – leftclickben Apr 16 '13 at 17:55

4 Answers4

1

If I use the sleep function, it simply delays loading the page. I want the echo to appear and then a redirect to occur.

You can use good old HTTP headers to accomplish this:

header('Refresh: 10; URL=usermainpage.php');

Remember that headers must be sent before any other output, otherwise a "headers already sent" error will be triggered. To control output you can take a look at the ob_* (output buffer) function family.

Shoe
  • 74,840
  • 36
  • 166
  • 272
1

Headers must be output to the browser before outputting any text. If you output text first, you've lost your opportunity to send headers because the body of the page must always be output last.

If you wish to redirect the browser after displaying some text, you'll need to utilize either Javascript, or a META redirect:

Javascript

<script type="text/javascript">
  setTimeout(function() {
    window.location = 'usermainpage.php';
  }, 2000);
</script>

Replace the value 2000 with the number of milliseconds to wait before re-direct. 2000 = 2 seconds, 10000 = 10 seconds.

META Tag

<meta http-equiv="refresh" content="5; URL='usermainpage.php'">

Set the number 5 to the number of seconds to wait before re-directing.

Joshua Burns
  • 8,268
  • 4
  • 48
  • 61
1

This is really gross and you shouldn't do this. Just use a 302 redirect and skip the useless page in-between:

header('Location: usermainpage.php', true, 302);

However, you can set the "Refresh" header, which is non-standard but very old and understood by all browsers.

header('Refresh: 10;URL=usermainpage.php');

10 is the delay in seconds before redirecting.

However, in case the browser does not understand this header, you should include a link on the page to the destination page.

More information from Stackoverflow and Wikipedia.

Community
  • 1
  • 1
Francis Avila
  • 31,233
  • 6
  • 58
  • 96
0

You would need to do a redirection with javascript or using a meta-refresh redirect, if you want to do it after page load.

Mike Brant
  • 70,514
  • 10
  • 99
  • 103