I want to hold an echo function for 4 seconds and after that refresh the php page but instead sleep() function holds all of echo functions and then refresh the page. this is my code how can I fix this ?

if($sql_new!='')
{
  $_SESSION["count"]="";

  echo "<div class='cleaner h30'></div>";
  echo "<b'>Inbox informations Submitted successfully!</b>";
  sleep(4);
  echo "<meta http-equiv=\"refresh\" content=\"0;URL=inbox.php\">";    
}
有帮助吗?

解决方案

echo "<meta http-equiv=\"refresh\" content=\"4;URL=inbox.php\">";

change 0 to 4 can do this

其他提示

Remove the sleep code from your PHP - it is just delaying the output of the HMTL to the browser. It is the meta tag in the code that needs to be updated to trigger to the browser to refresh the page 4 seconds after the page has loaded.

echo "<meta http-equiv=\"refresh\" content=\"4;URL=inbox.php\">";

Just another solution: there IS a way to archive what you were triying to do. YOu just have to make sure that the content you echo'ed before the sleep is already sent to the browser before you sleep. YOu can do so by:

  • calling flush()
  • making sure no output buffering is active, neither in PHP nor in your webserver
  • disable compressed responses in your webserver config
  • disable deflated responses in your webserver config
  • disable caching mechanisms that may be present.

Not saying you should do this, just you can.

Here is something I worked up to redirect. Seems to work fine and thought I would share.

DelayTime(15); 

echo "<script>window.top.location.reload();</script>";

The DelayTime Function:

function DelayTime($secs){
      $now = time();
      while($now < ($now + $sec)){ 
        $now = time();
        if($now >= ($now + $sec)){ break; }
      }
      return true;
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top