PHP Redirect (Header) error

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
Hopps
Forum Newbie
Posts: 1
Joined: Sun Apr 24, 2011 8:48 pm

PHP Redirect (Header) error

Post by Hopps »

Hello Everyone,

I am new to PHP but not really new to building websites. I've spent several hours looking up problems with this code but cannot get it to redirect properly. If someone would please take a look and identify my problem for me I would appreciate it. Everything I've read says there's white spaces that causes this code to break yet the PHP book I'm reading says PHP ignores white spaces. Please help me. This is the code I'm using from the sample I found. It is a registration form and the info gets put in the DB but the redirect fails and gives me this error.

Warning: Cannot modify header information - headers already sent by (output started at /home/beggarsnight/public_html/register.php:9) in /home/beggarsnight/public_html/register.php on line 60

<body>
<?php

// dbConfig.php is a file that contains your
// database connection information. This
// tutorial assumes a connection is made from
// this existing file.
include ("dbConfig.php");

$today = getdate();

//Input vaildation and the dbase code
if ( $_GET["op"] == "reg" )
{
$bInputFlag = false;
foreach ( $_POST as $field )
{
if ($field == "")
{
$bInputFlag = false;
}
else
{
$bInputFlag = true;
}
}
// If we had problems with the input, exit with error
if ($bInputFlag == false)
{
die( "Problem with your registration info. "
."Please go back and try again.");
}

// Fields are clear, add user to database
// Setup query
$q = "INSERT INTO `tblUserMain` (`mainUserName`,`mainPassWord`,`mainUserEmail`) "
."VALUES ('".$_POST["username"]."', "
."PASSWORD('".$_POST["password"]."'), "
."'".$_POST["email"]."') ";
// Run query
$r = mysql_query($q);

// Make sure query inserted user successfully
if ( !mysql_insert_id() )
{
die("Error: User not added to database.");
}
else
{
// Redirect to thank you page.
//Header('Location: register.php?op=thanks'); //####### THIS IS THE LINE THAT IS BROKEN ACCORDING TO THE MESSAGE
}
} // end if


//The thank you page
elseif ( $_GET["op"] == "thanks" )
{
echo "<h2>Thanks for registering!</h2>";
}

//The web form for input ability
else
{
echo "<form action=\"?op=reg\" method=\"POST\">\n";
echo "Username: <input name=\"username\" MAXLENGTH=\"16\"><br />\n";
echo "Password: <input type=\"password\" name=\"password\" MAXLENGTH=\"16\"><br />\n";
echo "Email Address: <input name=\"email\" MAXLENGTH=\"25\"><br />\n";
echo "<input type=\"submit\">\n";
echo "</form>\n";
}
// EOF
?>
</body>
</html>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: PHP Redirect (Header) error

Post by califdon »

You have sent a <body> HTML tag right at the beginning, therefore you cannot later send a header. In other words, you have already sent something to the browser, so it wouldn't recognize a header later. Either postpone sending the <body> tag (what about all the preceding tags, beginning with the DOCTYPE??) or use a different kind of redirect, such as sending a short Javascript redirect, like: <script type='text/javascript'>Window.location='register.php?op=Thanks';</script>
Post Reply