0

I work on a php installer.
What I want is that the Page sleeps 5 seconds, whereafter the Mainpage is loaded. I want a loading animation during this time.

My first working code without animation

$redir = str_replace('install/','',$redir);
        sleep(5);
        header( 'Location: ' . $redir . 'home' ) ;
    }

And this was my try with animation.

$redir = str_replace('install/','',$redir);

        ob_start();
        echo '<style>.register{display:none;}<style>';
        ob_flush();
        echo '<h1 class="loading">loading...</h1>';
        ob_flush();
        sleep(5);
        ob_flush();
        header( 'Location: ' . $redir . 'home' ) ;
    }

I think that it is wrong because it's not working.

Sep Roland
  • 33,889
  • 7
  • 43
  • 76
Ron Ny
  • 85
  • 2
  • 8

2 Answers2

0

You can't output some data and redirect after using PHP, from doc :

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP

You can use refresh header or Javascript refresh to achieve what you want.

Example with refresh header (if you don't output before) :

<?php
header('Refresh: 5;URL='. $redir);
echo '<style>.register{display:none;}<style><h1 class="loading">loading...</h1>';

Or with Javascript redirect :

<?php
echo ' 
    <script type="text/javascript">
     window.setTimeout(function(){
       location.href="'. $redir .'";
     },5000);
    </script>
    <style>.register{display:none;}<style><h1 class="loading">loading...</h1>';
Vincent Decaux
  • 9,857
  • 6
  • 56
  • 84
0

If you want load some page; and after 5 seconds user redirect, php isn't good choice for this! sleeping 5 seconds in php mean before rendering HTML, user wait 5 seconds to load page! you should do this using javascript (or jquery) , add this code to end of your page:

<script>
setTimeout(function() {
    window.location.href='new_url';
}, 5000);
</script>
aidinMC
  • 1,415
  • 3
  • 18
  • 35