Need Help !! Getting Error in sending headers

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
sree78
Forum Commoner
Posts: 39
Joined: Tue May 20, 2003 11:38 am

Need Help !! Getting Error in sending headers

Post by sree78 »

Hi I am creating a quiz module where every new user have to register before they get into the quiz page. Their Information will be stored in a database and as well as the results of the quiz. My code is as below:-


<?php


$first_name = pg_escape_string($first_name);
$last_name = pg_escape_string($last_name);
$email = pg_escape_string($email);
$birthdate = $month.$day;

$sql = "SELECT *
FROM person
WHERE first_name='$first_name' AND last_name='$last_name'
AND birthdate = '$birthdate'";

$result = pg_query($sql);
$row = pg_fetch_row($result);
if ($row == ""):

//Add
$sql = "SELECT org_id
FROM org
WHERE name = '$org'";
$result = pg_query($sql);
$row = pg_fetch_row($result);
$org_id = $row[0];
$sql = "INSERT INTO person
(first_name, last_name, email, birthdate, status, org_id)
VALUES ('$first_name', '$last_name', '$email', '$birthdate', '$status', '$org_id')";

print $sql;
$result= pg_query($sql);



header ("Location:$redirect");
setcookie ("reg_id","$person_id", 0, "/");

else:
//User exists. Send 'em back
$errstr = 'Only one person can be registered with the same name and ';
$errstr .= 'birthday. Someone has already logged in under your name. ';
$errstr .= 'If you have registered before, go to the short registration ';
$errstr .= 'form. If this is your first time, type your nickname or add ';
$errstr .= 'a middle initial in the "First Name" box.';
endif;
?>

If you can see the code in BOLD.. I cannot get the page to redirect once the user registers but instead I am getting an error like this below:-

Warning: Cannot modify header information - headers already sent by


At the end of my html page I have this piece of code which tells the page to redirect:-

Code: Select all

<input type=hidden name="redirect" value="quiz.php">
<table cellpadding="10">
  <tr><td align=center><input type=button value="Register" onClick="largeReg(document.reg);"></td></tr>
</table>
</form>
Any help would be highly appreciated!!!
?>
User avatar
brewmiser
Forum Commoner
Posts: 74
Joined: Mon Aug 18, 2003 12:50 pm
Location: Dallas, TEXAS

Post by brewmiser »

Is there any HTML code above this?
qads
DevNet Resident
Posts: 1199
Joined: Tue Apr 23, 2002 10:02 am
Location: Brisbane

Post by qads »

User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

Allow me to elaborate: That error is caused by because you are trying to send header information AFTER you have sent output to the browser. Usually this is a simple case of reordering a statement or removing white space before and/or after the PHP start and end tags, however you can also use output buffering with ob_start() and ob_end_flush() which will allow you to place headers and content in any order you want to. Note output buffering was originally designed to solve HTTP header errors. Although ob_end_flush() isn't needed in MOST cases because it is called automatically at the end of script execution by PHP itself when output buffering is turned on either in the php.ini or by calling ob_start().

Code: Select all

<?php
    ob_start();
    if(!get_magic_quotes_gpc()) &#123;
        $first_name = pg_escape_string($first_name);
        $last_name = pg_escape_string($last_name);
        $email = pg_escape_string($email);
    &#125;
    $birthdate = $month . $day;
    $sql = "SELECT * FROM person WHERE first_name='$first_name' AND last_name='$last_name' AND birthdate='$birthdate'";
    $result = pg_query($sql) or die(pg_last_error());
    $row = pg_fetch_row($result) or die(pg_last_error());
    if(empty($row)) &#123;
        $sql = "SELECT org_id FROM org WHERE name='$org'";
        $result = pg_query($sql) or die(pg_last_error());
        $row = pg_fetch_row($result) or die(pg_last_error());
        $org_id = $row&#1111;0];
        $sql = "INSERT INTO person(first_name, last_name, email, birthdate, status, org_id) VALUES('" . $first_name . "', '" . $last_name . "', '" . $email . "', '" . $birthdate . "', '" . $status . "', '" . $org_id . "')";
        $result = pg_query($sql) or die(pg_last_error());
        setcookie('reg_id', $person_id, 0, '/');
        if(!headers_sent()) &#123;
            header ('Location: ' . $redirect);
            exit();
        &#125;
    &#125; else &#123;
        $errstr = 'Only one person can be registered with the same name and ';
        $errstr .= 'birthday. Someone has already logged in under your name. ';
        $errstr .= 'If you have registered before, go to the short registration ';
        $errstr .= 'form. If this is your first time, type your nickname or add ';
        $errstr .= 'a middle initial in the "First Name" box.';
    &#125;
?>
<INPUT TYPE="hidden" NAME="redirect" VALUE="quiz.php">
<TABLE cellpadding="10">
  <TR>
    <TD ALIGN=center><INPUT TYPE=button VALUE="Register" onClick="largeReg(document.reg);"></TD>
  </TR>
</TABLE></FORM>
<?php
    ob_end_flush();
?>
sree78
Forum Commoner
Posts: 39
Joined: Tue May 20, 2003 11:38 am

Post by sree78 »

Hi.. Kriek..

Thank you so much for your quick reply.. I have been out of the office and I have just tried your method. The page does not produce any errors but I am still not getting to the redirect page. The browser is blank white when I hit the register button.

Any help is highly appreciated...

Once again thanks
sree78
Forum Commoner
Posts: 39
Joined: Tue May 20, 2003 11:38 am

Post by sree78 »

I have managed to solve the problem... it was just a few editing... once again thanks a lot Kriek...
User avatar
Kriek
Forum Contributor
Posts: 238
Joined: Wed May 29, 2002 3:46 am
Location: Florida
Contact:

Post by Kriek »

No problem, glad I could be of assistance to you.
Post Reply