Page 1 of 1

Conditional Redirect

Posted: Sun Mar 22, 2009 11:43 am
by Foxy999
I want to have my page redirect to another page while a condition is true. Here is my code:

if($_POST[title] == 0 || $_POST[desc] == 0 || $_POST[name] == 0 || $_POST[email] == 0)
{
header("Location: http://www.google.com");
exit();
}

But I get this error:

Warning: Cannot modify header information - headers already sent by

I have looked at my PHP book and have found that whitespace before the php tags can be a problem, but I have opened it with several editors and have cleaned it up perfectly. I have looked at the line that the error returns and its at line 11, but at that line theres only the <?php tag. I have read online that I may be able to fix this problem by adding this to my configuration.php file:

$mosConfig_locale_debug = 0;
$mosConfig_locale_use_gettext = 0;

But I do not have a configuration.php, and there should be an easier way to do this. Here is my complete coding:

</head>
<body>
<?php
$con = mysql_connect(x, x, x);
if(!$con)
{
die('Cannot connect: ' .mysql_error());
}

if($_POST[title] == 0 || $_POST[desc] == 0 || $_POST[name] == 0 || $_POST[email] == 0)
{
header("Location: http://www.google.com");
exit();
}

mysql_select_db("yourcol1_ITEM", $con);
$sql = ("INSERT INTO SELL (`title`, `desc`, `name`, `email`)
VALUES ('$_POST[title]','$_POST[desc]','$_POST[name]','$_POST[email]')");

if (!mysql_query($sql,$con))
{
die(mysql_error()."<br><br>$sql");
}
echo "Complete_debug";
mysql_close($con);
?>
</body>
</html>

(Ignore the 'x' marks in the code)

Foxy

Re: Conditional Redirect

Posted: Sun Mar 22, 2009 11:46 am
by jayshields
You have HTML in your script before the header() call. In other words, remove </head><body> and any HTML output before it.

Re: Conditional Redirect

Posted: Sun Mar 22, 2009 12:03 pm
by Foxy999
jayshields wrote:You have HTML in your script before the header() call. In other words, remove </head><body> and any HTML output before it.
Thanks that worked, but I would like to be able to print an error message in the conditional before the header() function, but I cannot print anything on that page because I will get more errors, any advice?

I also changed the '==' to '===' because that are strings, I am new to php, coming from java (.equals()).

Re: Conditional Redirect

Posted: Sun Mar 22, 2009 12:21 pm
by Inkyskin
Why not print the error on the next page? Header redirects are instant, so you'd never get a chance to read it anyhow...

Re: Conditional Redirect

Posted: Sun Mar 22, 2009 12:55 pm
by Foxy999
Inkyskin wrote:Why not print the error on the next page? Header redirects are instant, so you'd never get a chance to read it anyhow...
I have it redirecting to the previous page, since its like a multi-step submitting process. I am not sure how to make it print an error on the previous page. I can probably figure it out but I have other things to work on.

Re: Conditional Redirect

Posted: Sun Mar 22, 2009 1:03 pm
by Inkyskin
Stick the error message (or error status to use as a condition) in a session variable or a $_GET variable :)