Page 1 of 2

Does header("location: www.newsite.com"); work AL

Posted: Wed Feb 01, 2006 7:53 am
by kaisellgren
Greetings! I'd really need this information. If someone has experience and would like to tell me that is the following code working always?

Code: Select all

header("location: http://www.newsite.com");
By always I mean different cases like cookie blocking and firewall and so on...

Posted: Wed Feb 01, 2006 7:59 am
by Roja
You need to add the http:// before the domain, but yes, that is the correct syntax.

Code: Select all

header("location: http://www.newsite.com");
Most proxies and firewalls prefer header redirects over meta redirects, so that should generally work.

Posted: Wed Feb 01, 2006 8:58 am
by kaisellgren
Oh yes. But I actually meant a way to redirect in x seconds. I'd like to have redirection in 5 seconds. Possible+?

Posted: Wed Feb 01, 2006 9:02 am
by raghavan20
how about using sleep(5) before the header redirect???

Posted: Wed Feb 01, 2006 9:10 am
by kaisellgren
Yes, but that doesn't echo text... But okay, I'll use it if there's no other way.

Posted: Wed Feb 01, 2006 9:15 am
by raghavan20
kaisellgren wrote:Yes, but that doesn't echo text... But okay, I'll use it if there's no other way.
what do you mean by echoing text??

Posted: Wed Feb 01, 2006 9:17 am
by kaisellgren
I mean,
5 secs left...
4 secs left...
3 secs left...
2 secs left...
1 sec left...
<- redirecting ->

Posted: Wed Feb 01, 2006 9:28 am
by raghavan20
I really do not have a clue why this does not work...

Code: Select all

<?php

$timeOut = 5;
for ($i = $timeOut; $i >=0; $i--){
	echo $i." secs left...";
	sleepNow();	
}

function sleepNow(){
	sleep(1);
}
?>

</pre>

Posted: Wed Feb 01, 2006 9:29 am
by feyd
Only a select few browsers and servers support a delay in the headers. To do a countdown, you must send a page with Javascript or a refresher that updates the "clock"

Posted: Wed Feb 01, 2006 10:06 am
by raghavan20
This should be an alternative...

Code: Select all

<div id = 'displayTime'></div>
<?php
$timeOut = 5;
echo <<<EOD
<script type = 'text/javascript'>
var timeOut = $timeOut;
alertTime();
function alertTime(){
	var i = 0;
	if (timeOut != 0){
		window.setTimeout("alertTime()", 1000);	
		document.getElementById("displayTime").innerHTML = timeOut + "seconds left..";
		timeOut--;
	}else{
		window.location.href = "http://www.yahoo.com";
	}
}
</script>
EOD;
?>

Posted: Wed Feb 01, 2006 10:30 am
by pilau
Yes but use of JS has it's own problems, like browser compatbility and users turning off their JS platforms..

Posted: Wed Feb 01, 2006 10:55 am
by Christopher
kaisellgren wrote:I mean,
5 secs left...
4 secs left...
3 secs left...
2 secs left...
1 sec left...
<- redirecting ->
You should probably use the Meta Refresh HTML tag in the HTML <head> block rather than header(). You can set it to redirect in 5 seconds AND have a countdown animation (GIF, Flash or Javascipt). With header() not output can be sent.

Posted: Wed Feb 01, 2006 1:08 pm
by Roja
arborint wrote:You should probably use the Meta Refresh HTML tag in the HTML <head> block rather than header(). You can set it to redirect in 5 seconds AND have a countdown animation (GIF, Flash or Javascipt). With header() not output can be sent.
The meta refresh *does* cause problems with some proxies. (I've heard reports that it causes issues on AOL's browser as well, but I haven't ever been able to validate).

Posted: Wed Feb 01, 2006 2:10 pm
by Christopher
Roja wrote:The meta refresh *does* cause problems with some proxies. (I've heard reports that it causes issues on AOL's browser as well, but I haven't ever been able to validate).
Yeah, meta refresh is not as foolproof as setting headers because it is browser dependent rather than a HTTP thing. But it is pretty universally supported and will probably be fine for uses like this.

cant with php

Posted: Wed Feb 01, 2006 3:22 pm
by yakasha
You can't do what you're wanting to do with php for a couple reasons.

1) Nothing is sent to the the client until after your script is completely finished executing. This means putting Sleep() calls in your code will only accomplish one thing: Your viewers will think your site is really slow.

2) The header() function sends a header to the client's browser. The Location header tells the browser to go to a different page. It won't display any other content you send. As soon as the client sees the Location header, its done.

You'll have to do this with either a meta tag (for a simple redirection after 5 seconds), or with Javascript if you want a nifty message. Something like this will do it:

Code: Select all

<html>
 <head>
  <script language="javascript">
function Redirect_Countdown(timer)
{
  if( timer == 0 )
  {
    window.location='http://yourlocation.com/';
  }
  else
  {
    timerText = document.getElementById('timerText');
    timerText.innerHTML = timerText.innerHTML + timer + " seconds remaining...<br>\n";
    setTimeout('Redirect_Countdown(' + (timer-1) + ')', timer*1000);
  }
}
  </script>
 </head>
 <body onLoad="Redirect_Countdown(5);">
  <div id="timerText"></div>
 </body>
</html>