1

I've tried solutions from below links. But none of them give luck.

php-output-text-before-sleep

php-output-data-before-and-after-sleep

php-time-delay-using-ob-flush-with-loading-message

Actually below is my script.

<?php
include 'ini/INI.class.php';
$CompIP = $_SERVER['REMOTE_ADDR'];
$inidata = (parse_ini_file("guard.ini",true));
$atm = time()-$inidata["guard"][$CompIP];
if ($atm>60) { $atm = 1; }
echo "<p>You will be redirected to report page in <span id='counter'>" .     $atm . "</span> second(s).</p>"; 
sleep($atm);
//my 
//100 
//line 
//user report from mysql
$ini = new INI('guard.ini');
$ini->data['guard'][$CompIP] = time();
$ini->write();
?>

Still I get the whole content include 'You will be redirected to .......' after $atm (pause seconds) seconds.

My Workaround

<?php
include 'ini/INI.class.php';
$CompIP = $_SERVER['REMOTE_ADDR'];
$inidata = (parse_ini_file("guard.ini",true));
$atm = (time()-(isset($inidata["guard"][$CompIP]) ? $inidata["guard"][$CompIP] : 0));
if ($atm<60)
{
echo "<p>You will be redirected to report page in <span id='counter'>" . (60-$atm) . "</span> second(s).</p> <script type='text/javascript'> function countdown() { var j = document.getElementById('counter'); j.innerHTML = parseInt(j.innerHTML)-1; if (parseInt(j.innerHTML)<=0) { j.innerHTML = 0; location.href = 'tr.php'; } } for (i=1;i<=" . (60-$atm) . ";i++) { setTimeout(function(){ countdown(); },i*1000); } </script>";
} else { mysqlreport; $ini = new INI('guard.ini');
$ini->data['guard'][$CompIP] = time();
$ini->write();
}
?> 
Community
  • 1
  • 1
Dhay
  • 585
  • 7
  • 29

1 Answers1

1

You can use JavaScript for this purpose and pass variables from PHP to JavaScript simply by writing the JavaScript code inside "echo". I think something like this will do the trick.

For redirection, the below example will give you an idea:

<?php

$url = "http://google.com";
$step = "1000";
$start = 12;

echo 'Redirection After <h1 id="counter">'.$start.'</h1> ';

echo '
  <script>

    var x = '.$start.';

    setInterval(function(){

     if(x==1){
        window.location = "'.$url.'";
     }

      document.getElementById("counter").innerHTML = x;

      x--;

   }, "'.$step.'");

  </script>';
?>

As for your content that you want to output, just place a tag and with JavaScript. Also, you can update it every 10 seconds; the technique is this, how you do it is up to you.

Brian
  • 14,610
  • 7
  • 35
  • 43
Aram Rafeq
  • 41
  • 8
  • AramRafeq, Your solution is worked. Actually I didn't try to redirect to another page. 'You will be redirected..' is just a fake info to viewers. I want 'You will be redirected..' text for a while (it records previous load time and if current time is less than 60 seconds then the 'You will be redirected..' text shows until 60 seconds gap meets) and after that to show mysql results (as I [said](http://stackoverflow.com/questions/31344586/php-echo-text-then-sleep-10-seconds-then-echo-another-text#comment50688816_31344586) to iam-decoder). Thank you for your efforts. – Dhay Jul 11 '15 at 03:43
  • or you are giving this solution means we can expect the result I wanted only by javascript and redirecting to another page? – Dhay Jul 11 '15 at 03:55
  • I decided to take another workaround and I've updated my script. Thank you everyone who tried to give solution for me. – Dhay Jul 11 '15 at 07:18
  • And actually it includes a solution as you've given Aram Rafeq. – Dhay Jul 11 '15 at 07:26