0

I'm new to programming in PHP. Kindly bear me...

when using header function, refresh option is not working when using variable from post method(dynamically). when hard-coded the number its working. I tried different options. You can see my whole code here.not succeeded to make refresh work dynamically. Can someone help?

<?php
if($_POST['time']>0) {
     $t = $_POST['time'];
     $u =$_POST['url'];
     echo "You will be redirected to the " .$u . " website in " .$t. "seconds";
     //header("refresh:5; url=http://www.google.com");

     //header("refresh:($_POST['time']);url=($_POST['url'])"); 
     header('refresh: ' .$t);
     header('Location: '.$u);
     exit;       
}
?>
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
user1479431
  • 369
  • 1
  • 5
  • 10

5 Answers5

1

You have to call header() before any output like your echo statement. It simply will not work after anything has been output to the browser.

Scott Saunders
  • 29,840
  • 14
  • 57
  • 64
1

The Refresh header isn't just a number, it's supposed to contain the url as well. The format is:

Refresh: 5; url=http://something.local/

The Location header should be absent then.

header("Refresh: $t; url=$u");

See also http://en.wikipedia.org/wiki/HTTP_refresh

mario
  • 144,265
  • 20
  • 237
  • 291
  • 0) { $t = $_POST['time']; $u =$_POST['url']; header("Refresh: $t; url=$u"); echo "You will be redirected to the " .$u . " website in " .$t. "seconds"; exit; } ?> – user1479431 Jun 25 '12 at 22:52
1

You cannot call the php header function after any output has already been printed to the screen. If you remove that echo statement it should work properly.

Matt Dodge
  • 10,833
  • 7
  • 38
  • 58
0

This is more reliably done with a JavaScript redirect:

echo "<p>You will be redirected in ".$t." seconds.</p>";
echo "<p>Click <a href=\"".$u."\">here</a> to go immediately.</p>";
echo "<script type=\"text/javascript\">setTimeout(function() {location.href = ".json_encode($u).";},".$t."000);</script>";
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
0
if ($_POST['time'] && $_POST['url'])
{
    $time = (integer)$_POST['time'];
    $url = strip_tags($_POST['url']);
    echo "You will be redirected to the " . $url . " website in " . $time . "seconds";
    header('Refresh:' . $time . ';url=' . $url);
}
mintobit
  • 2,363
  • 2
  • 15
  • 15