Conditional Redirect

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

Post Reply
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Conditional Redirect

Post 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
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Re: Conditional Redirect

Post by jayshields »

You have HTML in your script before the header() call. In other words, remove </head><body> and any HTML output before it.
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Conditional Redirect

Post 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()).
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Conditional Redirect

Post 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...
Foxy999
Forum Commoner
Posts: 45
Joined: Sat Mar 21, 2009 11:50 am

Re: Conditional Redirect

Post 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.
User avatar
Inkyskin
Forum Contributor
Posts: 282
Joined: Mon Nov 19, 2007 10:15 am
Location: UK

Re: Conditional Redirect

Post by Inkyskin »

Stick the error message (or error status to use as a condition) in a session variable or a $_GET variable :)
Post Reply