php - URL forward

rh71

No Lifer
Aug 28, 2001
52,844
1,049
126
Have a php script that processes a form... and I need it to forward to a specific URL after X seconds. Can't use META REFRESH because there's a condition on the page so it would only forward if it met one of the conditions.

What's the code to forward after X seconds or even simply go to a page immediately, via php ?
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
PHP is server-side, so you can't. Javascript would be what you'd use. I dunno how to do a timer with Javascript but when you reach zero (and your other conditions are met), you'd just do window.location = "<url>".

To immediately forward with PHP you could send an HTTP Location header: header("Location: <url>"). But remember that you have to do it before you send ANY content to the browser.
 

Zugzwang152

Lifer
Oct 30, 2001
12,134
1
0
you cannot use a server-side scripting language to redirect pages after output has been written to the browser.

using javascript in this case would be your best option.

alternatively, if all processing and data validation is done server-side, you can use sleep to delay execution, and then use header to redirect.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0

Originally posted by: DnaJ

That would be the correct way to do it.

How do you figure? There are a number of ways to do it, none of them incorrect. Just depends on the specifics (which he didn't go into very much).
 

DannyBoy

Diamond Member
Nov 27, 2002
8,820
2
81
www.danj.me
Originally posted by: BingBongWongFooey

Originally posted by: DnaJ

That would be the correct way to do it.

How do you figure? There are a number of ways to do it, none of them incorrect. Just depends on the specifics (which he didn't go into very much).



What's the code to forward after X seconds or even simply go to a page immediately, via php ?

 

rh71

No Lifer
Aug 28, 2001
52,844
1,049
126
thx guys, for some reason the Location nor Sleep worked. Location didn't do anything at all (as if it weren't even there) while Sleep delayed the processing of the entire page altogether, not dependent on where it was placed. I've found that php is a bit weird depending on how it was setup on the host and the version.

I decided to do the code processing on the output page (where it was going to be directed). I'm sure this is the way it's supposed to be anyway. Didn't know much about php before but I guess a little trouble goes a long way. ;) Thanks.