Page 1 of 1

page redirect on error

Posted: Mon Oct 18, 2004 10:24 am
by Handler
Greetings!

Learning learning learning! Ok .. my index page I input the date through pulldowns. Once the submit is sent I go to antoher page to validate the date. Now, if there's an error I'd like it to be know and then redirect back to the index page.

Code: Select all

<?php
$byear = $_POST['byear'];
$bday = $_POST['bday'];
$bmon = $_POST['bmonth'];
echo "byear = ".$byear."<br>";
echo "bday = ".$bday."<br>";
echo "bmon = ".$bmon."<br>";
if (checkdate( $bmon, $bday, $byear)) {
	echo "Date is good!";
	} else {
					echo "Date is bad";
					}
?>
Things are getting better everyday!

Thanks in advance!

Handler

Posted: Mon Oct 18, 2004 10:27 am
by kettle_drum

Code: Select all

header("Location: index.php");

Posted: Mon Oct 18, 2004 11:05 am
by phpScott
remeber not to put anywhite space or echoing before you use the header redirection method or you will be posting agian i'm sure

Posted: Mon Oct 18, 2004 11:20 am
by Handler
Ok .. here's what I got!

Code: Select all

<?php
$byear = $_POST['byear'];
$bday = $_POST['bday'];
$bmon = $_POST['bmonth'];
echo "byear = ".$byear."<br>";
echo "bday = ".$bday."<br>";
echo "bmon = ".$bmon."<br>";
if (checkdate( $bmon, $bday, $byear)) {
	echo "Date is good!";
	} else {
					echo "Date is bad";
					echo "Sending you back to do it again!";
header("Location: index.php");
					}
?>
i get this error:

Warning: Cannot modify header information - headers already sent by (output started at C:\www\test.php:7) in C:\www\test.php on line 20

Interesting! So once this little bug is worked out when it hits a bad date it'll just shoot back to the index page. Is there a pause/timer/wait function in php? I didn't see one. I work with mainframe work flow and we have this function called wait. It'll wait for a file, wait for a number of seconds, wait for a user to OK it. Is there something similar?

Thanks!

Posted: Mon Oct 18, 2004 11:24 am
by timvw
it's already mentionned in this thread, and RTFM [php_man]header[/php_man]

Posted: Mon Oct 18, 2004 11:26 am
by feyd

Posted: Mon Oct 18, 2004 8:35 pm
by Handler
Ok .. i got it .. I need to do all my checks at the beginning of the script and not as it flows down the page. Thanks for that link feyd. I really didn't need to get slammed with the RTFM comment. I think it was uncalled for! I did go to the doc about it, just that it didn't particularly make much sense to me.

After googling for awhile I found that I'd probably be better off testing it while on that page using a function.

I'm still putting this little tid bit of solatious info in my notebook!

Thanks to all that got me on my way!

Posted: Tue Oct 19, 2004 6:59 am
by kettle_drum
Its all good. Just remember next time when you send any header, or cookie or session - that you cant have sent ANY text or white space to the user before hand. There is a good few posts a week on people asking why they get this error - so it gets a bit annoying after a while.