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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

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

Post 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...
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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.
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post 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+?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

how about using sleep(5) before the header redirect???
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

Yes, but that doesn't echo text... But okay, I'll use it if there's no other way.
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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??
User avatar
kaisellgren
DevNet Resident
Posts: 1675
Joined: Sat Jan 07, 2006 5:52 am
Location: Lahti, Finland.

Post by kaisellgren »

I mean,
5 secs left...
4 secs left...
3 secs left...
2 secs left...
1 sec left...
<- redirecting ->
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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"
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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;
?>
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Yes but use of JS has it's own problems, like browser compatbility and users turning off their JS platforms..
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
Roja
Tutorials Group
Posts: 2692
Joined: Sun Jan 04, 2004 10:30 pm

Post 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).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
yakasha
Forum Newbie
Posts: 10
Joined: Wed Aug 03, 2005 1:17 pm
Location: Las Vegas, NV
Contact:

cant with php

Post 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>
Post Reply